# frozen_string_literal: true RSpec.describe StrikeMe::BTC do context 'create from integer' do it 'creates using #btc' do btc = 314.btc expect(btc).to be_a described_class expect(btc.sats).to eq 314 * 100_000_000 end it 'creates using #sats' do btc = 314.sats expect(btc).to be_a described_class expect(btc.sats).to eq 314 end end context 'create from float' do it 'creates using #btc' do btc = 314.85.btc expect(btc).to be_a described_class expect(btc.sats).to eq 31_485_000_000 end it 'creates using #sats' do btc = 314.85.sats expect(btc).to be_a described_class expect(btc.sats).to eq 315 end end it 'properly formats to string' do expect(described_class.new(234_522).to_s).to eq '0.00234522' end it 'formats properly in json structures' do btc = described_class.new(234_522) expect({ amount: btc }.to_json).to eq '{"amount":"0.00234522"}' end context 'arithmetic' do it 'supports multiplication with numbers' do btc1 = described_class.new 234_529 btc2 = btc1 * 3 btc3 = btc2 * 2.9 expect(btc2).to be_a described_class expect(btc3).to be_a described_class expect(btc2).not_to eq btc1 expect(btc2).not_to eq btc3 expect(btc1.sats).to eq 234_529 expect(btc1).to eq 234_529 expect(btc2).to eq 703_587 expect(btc3).to eq 2_040_402 end it 'supports division with numbers' do btc1 = described_class.new 34_529 btc2 = btc1 / 3 btc3 = btc2 / 2.9 expect(btc2).to be_a described_class expect(btc3).to be_a described_class expect(btc2).not_to eq btc1 expect(btc2).not_to eq btc3 expect(btc1.sats).to eq 34_529 expect(btc1).to eq 34_529 expect(btc2).to eq 11_509 expect(btc3).to eq 3_968 end it 'supports addition with other BTC objects' do btc1 = described_class.new(34_529) btc2 = described_class.new(2_556) btc3 = btc1 + btc2 expect { btc1 + 23 }.to raise_error StrikeMe::BTC::Error expect(btc3).to be_a described_class expect(btc3).to eq 37_085 end it 'supports subtraction with other BTC objects' do btc1 = described_class.new 34_529 btc2 = described_class.new 2_556 btc3 = btc1 - btc2 expect { btc1 - 23 }.to raise_error StrikeMe::BTC::Error expect(btc3).to be_a described_class expect(btc3).to eq 31_973 end end it 'is consistent for small values' do # if floor is used instead of round, the small number 3, multiplied by 100 million can result in 2.99999999 leading to 2 string = 3.sats.to_s expect(string).to eq '0.00000003' expect(string.to_f.btc.sats).to eq 3 end end