Skip to content

Commit

Permalink
#51 Handle corner case of values that look like numbers but should be…
Browse files Browse the repository at this point in the history
… treated as strings
  • Loading branch information
rogerluan committed Dec 4, 2023
2 parents ee3e79f + 2b3d070 commit 35f9cff
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/arkana/models/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ def self.new(string_value:)
when "true", "false"
BOOLEAN
when /^\d+$/
# Handles cases like "0001" which should be interpreted as strings
return STRING if string_value.to_i.to_s != string_value
# Handle int overflow
return STRING if string_value.to_i > (2**31) - 1
INTEGER
else
STRING
Expand Down
40 changes: 40 additions & 0 deletions lib/arkana/templates/arkana_tests.swift.erb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,46 @@ final class <%= @namespace %>Tests: XCTestCase {
XCTAssertEqual(<%= @namespace %>.decode(encoded: encoded, cipher: salt), 42)
}

func test_decodeIntValueWithLeadingZeroes_shouldDecodeAsString() {
<% int_with_leading_zeroes_key = "0001" %>
<% secret = generate_test_secret(key: int_with_leading_zeroes_key) %>
let encoded: [UInt8] = [
<%= secret.encoded_value %>

]
XCTAssertEqual(<%= @namespace %>.decode(encoded: encoded, cipher: salt), "0001")
}

func test_decodeMassiveIntValue_shouldDecodeAsString() {
<% int_with_massive_number_key = "92233720368547758079223372036854775807" %>
<% secret = generate_test_secret(key: int_with_massive_number_key) %>
let encoded: [UInt8] = [
<%= secret.encoded_value %>

]
XCTAssertEqual(<%= @namespace %>.decode(encoded: encoded, cipher: salt), "92233720368547758079223372036854775807")
}

func test_decodeNegativeIntValue_shouldDecodeAsString() {
<% negative_int_key = "-42" %>
<% secret = generate_test_secret(key: negative_int_key) %>
let encoded: [UInt8] = [
<%= secret.encoded_value %>

]
XCTAssertEqual(<%= @namespace %>.decode(encoded: encoded, cipher: salt), "-42")
}

func test_decodeFloatingPointValue_shouldDecodeAsString() {
<% float_key = "3.14" %>
<% secret = generate_test_secret(key: float_key) %>
let encoded: [UInt8] = [
<%= secret.encoded_value %>

]
XCTAssertEqual(<%= @namespace %>.decode(encoded: encoded, cipher: salt), "3.14")
}

func test_encodeAndDecodeValueWithDollarSign_shouldDecode() {
<% dollar_sign_key = "real_$lim_shady" %>
<% secret = generate_test_secret(key: dollar_sign_key) %>
Expand Down
8 changes: 8 additions & 0 deletions spec/fixtures/.env.fruitloops
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ BoolAsBoolTrueKey = true
BoolAsBoolFalseKey = false
IntAsStringKey = "42"
IntAsNumberKey = 42
IntWithLeadingZeroesAsStringKey = "0001"
IntWithLeadingZeroesAsNumberKey = 0001
MassiveIntAsStringKey = "92233720368547758079223372036854775807"
MassiveIntAsNumberKey = 92233720368547758079223372036854775807
NegativeIntAsStringKey = "-42"
NegativeIntAsNumberKey = -42
FloatAsStringKey = "3.14"
FloatAsNumberKey = 3.14
SecretWithDollarSignEscapedAndAndNoQuotesKey = real_\$lim_shady
SecretWithDollarSignEscapedAndDoubleQuoteKey = "real_\$lim_shady"
SecretWithDollarSignNotEscapedAndSingleQuoteKey = 'real_$lim_shady'
Expand Down
8 changes: 8 additions & 0 deletions spec/fixtures/swift-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ global_secrets:
- BoolAsBoolFalseKey
- IntAsStringKey
- IntAsNumberKey
- IntWithLeadingZeroesAsStringKey
- IntWithLeadingZeroesAsNumberKey
- MassiveIntAsStringKey
- MassiveIntAsNumberKey
- NegativeIntAsStringKey
- NegativeIntAsNumberKey
- FloatAsStringKey
- FloatAsNumberKey
- SecretWithDollarSignEscapedAndAndNoQuotesKey
- SecretWithDollarSignEscapedAndDoubleQuoteKey
- SecretWithDollarSignNotEscapedAndSingleQuoteKey
Expand Down
50 changes: 50 additions & 0 deletions spec/models/type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,55 @@
expect(subject).to eq :string
end
end

context "when passing something that looks like a number" do
context "when it contains leading zeros" do
subject { described_class.new(string_value: "0001") }

it "returns :string" do
expect(subject).to eq :string
end
end

context "when its string representation is the same as its integer representation" do
subject { described_class.new(string_value: "1234567890") }

it "returns :integer" do
expect(subject).to eq :integer
end
end

context "when its string representation is different from its integer representation" do
subject { described_class.new(string_value: "1234567890.0") }

it "returns :string" do
expect(subject).to eq :string
end
end

context "when it contains a decimal point" do
subject { described_class.new(string_value: "3.14") }

it "returns :string" do
expect(subject).to eq :string
end
end

context "when it contains a comma" do
subject { described_class.new(string_value: "1,000") }

it "returns :string" do
expect(subject).to eq :string
end
end

context "when it contains a massive number" do
subject { described_class.new(string_value: "123456789012345678901234567890") }

it "returns :string" do
expect(subject).to eq :string
end
end
end
end
end

0 comments on commit 35f9cff

Please sign in to comment.