Skip to content

Commit

Permalink
Implement a basic test suite.
Browse files Browse the repository at this point in the history
Co-authored-by: Claude 3.5 Sonnet
  • Loading branch information
artob committed Jan 19, 2025
1 parent d5a37bc commit 2f5f1fe
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 2 deletions.
8 changes: 8 additions & 0 deletions lib/near/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ def initialize(id)
# @return [String]
attr_reader :id

##
# The balance as a Ⓝ-prefixed string.
#
# @return [String]
def inspect
"Ⓝ#{@id}"
end

##
# @return [String]
def to_s; @id; end
Expand Down
1 change: 1 addition & 0 deletions lib/near/balance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def self.from_near(s)

##
# @param [Numeric] quantity
# @raise [ArgumentError] if quantity is not a valid number
def initialize(quantity)
@quantity = case quantity
when BigDecimal then quantity
Expand Down
2 changes: 1 addition & 1 deletion lib/near/block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def initialize(height: nil, hash: nil)

##
# @return [Array<String>]
def to_args
def to_cli_args
return ['at-block-height', @height] if @height
return ['at-block-hash', @hash] if @hash
['now']
Expand Down
2 changes: 1 addition & 1 deletion lib/near/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,6 @@ def block_args(block)
when String then NEAR::Block.at_hash(block)
else raise "invalid block specifier: #{block.inspect}"
end
block.to_args
block.to_cli_args
end
end # NEAR::CLI
31 changes: 31 additions & 0 deletions spec/account_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This is free and unencumbered software released into the public domain.

require 'near'

RSpec.describe NEAR::Account do
describe '#initialize' do
it 'instantiates an account with the given ID' do
account = NEAR::Account.new('alice.near')
expect(account.id).to eq('alice.near')
end

it 'converts the input to a string' do
account = NEAR::Account.new(:'alice.near')
expect(account.id).to eq('alice.near')
end
end

describe '#inspect' do
it 'returns the account ID as a Ⓝ-prefixed string' do
account = NEAR::Account.new('alice.near')
expect(account.inspect).to eq('Ⓝalice.near')
end
end

describe '#to_s' do
it 'returns the account ID as a string' do
account = NEAR::Account.new('alice.near')
expect(account.to_s).to eq('alice.near')
end
end
end
79 changes: 79 additions & 0 deletions spec/balance_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# This is free and unencumbered software released into the public domain.

require 'near'

RSpec.describe NEAR::Balance do
describe '#initialize' do
context 'with valid input' do
it 'accepts an integer' do
balance = NEAR::Balance.new(100)
expect(balance.to_i).to eq(100)
end

it 'accepts a float' do
balance = NEAR::Balance.new(100.5)
expect(balance.to_f).to eq(100.5)
end

it 'accepts a rational' do
balance = NEAR::Balance.new(Rational(201, 2))
expect(balance.to_r).to eq(Rational(201, 2))
end

it 'accepts a decimal' do
balance = NEAR::Balance.new(BigDecimal('123.45'))
expect(balance.to_f).to eq(123.45)
end

it 'accepts a string' do
balance = NEAR::Balance.new('123.45')
expect(balance.to_f).to eq(123.45)
end
end

context 'with invalid input' do
it 'raises ArgumentError for invalid strings' do
expect { NEAR::Balance.new('invalid') }.to raise_error(ArgumentError)
end

it 'raises ArgumentError for nil' do
expect { NEAR::Balance.new(nil) }.to raise_error(ArgumentError)
end
end
end

describe '#inspect' do
it 'returns the balance as a Ⓝ-prefixed string' do
balance = NEAR::Balance.new(100.5)
expect(balance.inspect).to eq('Ⓝ100.5')
end
end

describe '#to_s' do
it 'returns the balance as a string' do
balance = NEAR::Balance.new(100.5)
expect(balance.to_s).to eq('100.5')
end
end

describe '#to_r' do
it 'returns the balance as a rational' do
balance = NEAR::Balance.new(Rational(201, 2))
expect(balance.to_r).to eq(Rational(201, 2))
end
end

describe '#to_i' do
it 'returns the balance as an integer' do
balance = NEAR::Balance.new(100.5)
expect(balance.to_i).to eq(100)
end
end

describe '#to_f' do
it 'returns the balance as a float' do
balance = NEAR::Balance.new(100)
expect(balance.to_f).to eq(100.0)
end
end
end
73 changes: 73 additions & 0 deletions spec/block_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# This is free and unencumbered software released into the public domain.

require 'near'

RSpec.describe NEAR::Block do
describe '.now' do
it 'instantiates a block representing the current state' do
block = NEAR::Block.now
expect(block.height).to be_nil
expect(block.hash).to be_nil
end
end

describe '.at_height' do
it 'instantiates a block for a specific height' do
block = NEAR::Block.at_height(185_299_288)
expect(block.height).to eq(185_299_288)
expect(block.hash).to be_nil
end

it 'converts the height to an integer' do
block = NEAR::Block.at_height('185299288')
expect(block.height).to eq(185_299_288)
end
end

describe '.at_hash' do
it 'instantiates a block for a specific hash' do
block = NEAR::Block.at_hash('8Y5HyGLa5oX42bWm21neXTNLTQrE4yhGMWs4GLhTywzk')
expect(block.hash).to eq('8Y5HyGLa5oX42bWm21neXTNLTQrE4yhGMWs4GLhTywzk')
expect(block.height).to be_nil
end

it 'converts the hash to a string' do
block = NEAR::Block.at_hash(:'8Y5HyGLa5oX42bWm21neXTNLTQrE4yhGMWs4GLhTywzk')
expect(block.hash).to eq('8Y5HyGLa5oX42bWm21neXTNLTQrE4yhGMWs4GLhTywzk')
end
end

describe '#to_cli_args' do
it 'returns ["now"] for the current block' do
block = NEAR::Block.now
expect(block.to_cli_args).to eq(['now'])
end

it 'returns height arguments for height-based blocks' do
block = NEAR::Block.at_height(185_299_288)
expect(block.to_cli_args).to eq(['at-block-height', 185_299_288])
end

it 'returns hash arguments for hash-based blocks' do
block = NEAR::Block.at_hash('8Y5HyGLa5oX42bWm21neXTNLTQrE4yhGMWs4GLhTywzk')
expect(block.to_cli_args).to eq(['at-block-hash', '8Y5HyGLa5oX42bWm21neXTNLTQrE4yhGMWs4GLhTywzk'])
end
end

describe '#to_s' do
it 'returns "now" for the current block' do
block = NEAR::Block.now
expect(block.to_s).to eq('now')
end

it 'returns the height for height-based blocks' do
block = NEAR::Block.at_height(185_299_288)
expect(block.to_s).to eq('185299288')
end

it 'returns the hash for hash-based blocks' do
block = NEAR::Block.at_hash('8Y5HyGLa5oX42bWm21neXTNLTQrE4yhGMWs4GLhTywzk')
expect(block.to_s).to eq('8Y5HyGLa5oX42bWm21neXTNLTQrE4yhGMWs4GLhTywzk')
end
end
end

0 comments on commit 2f5f1fe

Please sign in to comment.