Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for Variant type using map type #161

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions contracts/Test.aes
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ main contract Test =
entrypoint test_list(a:list(int)) = a
entrypoint test_nested_list(a:list(list(int))) = a
entrypoint test_simple_map(a: map(int, bool)) = a
entrypoint test_string_map(a: map(string, string)) = a
entrypoint test_nested_map(a: map(int, map(int, bool))) = a
entrypoint test_template_map(a: template_map(int)) = a
entrypoint test_variants(a:really_t) = a
Expand Down
12 changes: 11 additions & 1 deletion src/FateComparator.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,18 @@ const bytesComparator = (a, b) => {
return listComparator(aList, bList)
}

const stringComparator = (a, b) => {
const as = a.toString()
const bs = b.toString()

if (as.length === bs.length) {
return as.localeCompare(bs)
}

return as.length - bs.length
}

const intComparator = (a, b) => Number(BigInt(a) - BigInt(b))
const stringComparator = (a, b) => a.toString().localeCompare(b.toString())
const boolComparator = (a, b) => a - b
const bitsComparator = (a, b) => {
return (a < 0 || b < 0) ? -intComparator(a, b) : intComparator(a, b)
Expand Down
5 changes: 5 additions & 0 deletions tests/Encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ test('Encode map arguments', t => {
t.is(encoded, 'cb_KxHLN316Gy8BDn+vbmBO', 'test_simple_map({[7] = false})')
})

test('Encode map arguments with sorted keys', t => {
const encoded = encoder.encode(CONTRACT, 'test_string_map', [[["fo", "a"], ["s", "a"]]])
t.is(encoded, 'cb_KxFFPju5Gy8CBXMFYQlmbwVhMOIQaw==', 'test_string_map({["fo"] = "a", ["s"] = "a"})')
})

test('Encode map arguments by object', t => {
t.plan(1)
const encoded = encoder.encode(CONTRACT, 'test_simple_map', [{ 7: false }])
Expand Down
11 changes: 3 additions & 8 deletions tests/FateComparator.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const FTInt = FateTypeInt()
const FTBool = FateTypeBool()

test('Compare primitive types', t => {
t.plan(5)
t.plan(4)
t.deepEqual(
sort(FateTypeInt(), [3, new FateInt(3), 5, -7, 1, 0]),
[-7, 0, 1, 3, new FateInt(3), 5]
Expand All @@ -43,13 +43,8 @@ test('Compare primitive types', t => {
)

t.deepEqual(
sort(FateTypeString(), ['Z', 'abc', '~', 'a', 'bab', new FateString('bab'), 'B', 'ab', 'b', 'aa', 'abd', 'abcd']),
['~', 'a', 'aa', 'ab', 'abc', 'abcd', 'abd', 'b', 'B', 'bab', new FateString('bab'), 'Z']
)

t.deepEqual(
sort(FateTypeString(), ['Z', 'abc', '~', 'a', 'bab', 'B', 'ab', 'b', 'aa', 'abd', 'abcd']),
['~', 'a', 'aa', 'ab', 'abc', 'abcd', 'abd', 'b', 'B', 'bab', 'Z']
sort(FateTypeString(), ['Z', 'abc', '~', 'a', 'abcd', 'bab', new FateString('bab'), 'B', 'ab', 'b', 'aa', 'abd']),
['~', 'a', 'b', 'B', 'Z', 'aa', 'ab', 'abc', 'abd', 'bab', new FateString('bab'), 'abcd']
)

t.deepEqual(
Expand Down
1 change: 1 addition & 0 deletions tests/integration/encoder.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ test_encoder 'test_list([1, 2, 3, 5, 8, 13, 21])'
test_encoder 'test_list([42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42])'
test_encoder 'test_nested_list([[1,2],[3,4],[5,6]])'
test_encoder 'test_simple_map({[7] = false})'
test_encoder 'test_string_map({["fo"] = "a", ["s"] = "a"})'
test_encoder 'test_nested_map({[0] = {[0] = false}, [1] = {[1] = true}, [2] = {[8] = true}})'
test_encoder 'test_template_map({[17] = "abc"})'
test_encoder 'test_tuple((true, false))'
Expand Down