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

Bytes equality fix #67

Merged
merged 2 commits into from
Mar 21, 2024
Merged
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
14 changes: 14 additions & 0 deletions src/Gren/Kernel/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ function _Utils_eqHelp(x, y, depth, stack) {
}
//*/

if (typeof DataView === "function" && x instanceof DataView) {
var length = x.byteLength;

if (y.byteLength !== length) {
return false;
}

for (var i = 0; i < length; ++i) {
if (x.getUint8(i) !== y.getUint8(i)) {
return false;
}
}
}

if (Array.isArray(x) && x.length !== y.length) {
return false;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/src/Main.gren
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Test exposing (..)
import Test.Array as Array
import Test.Basics as Basics
import Test.Bitwise as Bitwise
import Test.Bytes as Bytes
import Test.Char as Char
import Test.CodeGen as CodeGen
import Test.Dict as Dict
Expand All @@ -28,6 +29,7 @@ main =
[ Array.tests
, Basics.tests
, Bitwise.tests
, Bytes.tests
, Char.tests
, CodeGen.tests
, Dict.tests
Expand Down
35 changes: 35 additions & 0 deletions tests/src/Test/Bytes.gren
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module Test.Bytes exposing (tests)

import Bytes exposing (Bytes)
import Bytes.Encode
import Expect
import Fuzz
import Test exposing (..)


encodeString : String -> Bytes
encodeString s =
Bytes.Encode.encode <|
Bytes.Encode.string s


encodeInt : Int -> Bytes
encodeInt i =
Bytes.Encode.encode <|
Bytes.Encode.signedInt8 i


tests : Test
tests =
describe "Byte comparison"
[ fuzz Fuzz.string "same strings are equal" <| \s ->
Expect.equal (encodeString s) (encodeString s)
, fuzz (Fuzz.intRange -128 127) "same ints are equal" <| \i ->
Expect.equal (encodeInt i) (encodeInt i)
, fuzz Fuzz.string "different strings are not equal" <| \s ->
Expect.notEqual (encodeString s) (encodeString (s ++ "a"))
, fuzz (Fuzz.intRange -100 100) "different ints are not equal" <| \i ->
Expect.notEqual (encodeInt i) (encodeInt (i + 1))
, fuzz2 Fuzz.string (Fuzz.intRange 1 127) "string and int are not equal" <| \s i ->
Expect.notEqual (encodeString s) (encodeInt i)
]
4 changes: 3 additions & 1 deletion tests/src/Test/Json.gren
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ customTests : Test
customTests =
let
jsonString =
"""{ "foo": "bar" }"""
"""
{ "foo": "bar" }
"""

customErrorMessage =
"I want to see this message!"
Expand Down