-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix type conversion for custom type definitions (#204)
Type conversion for custom types (enums) was broken e.g. ```go type LoyaltyTier string const ( Bronze LoyaltyTier = "bronze" ) ``` Test plan: `go test -timeout 30s -run ^(TestStringComparsigon|TestNumericComparsigon)$` Benchmark `convertToString` before: ![Screenshot 2024-07-12 at 4 54 13 PM](https://github.com/user-attachments/assets/97c7e431-1188-4102-80ee-ec88e335acbb) after: (note that the constant to string conversion was previously broken) ![Screenshot 2024-07-12 at 4 54 00 PM](https://github.com/user-attachments/assets/736e22d1-f4a8-4f6b-958b-2cc755c91dba) Benchmark `getNumericValue` before: ![Screenshot 2024-07-12 at 4 58 38 PM](https://github.com/user-attachments/assets/d956c7be-7f36-4cd2-b970-2466bddc91b2) after: ![Screenshot 2024-07-12 at 4 58 29 PM](https://github.com/user-attachments/assets/85ee00c2-becf-4070-a595-642a9af0f60f)
- Loading branch information
1 parent
9f37bc1
commit 3c50265
Showing
2 changed files
with
102 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package statsig | ||
|
||
import "testing" | ||
|
||
func TestStringComparsigon(t *testing.T) { | ||
eq := func(s1, s2 string) bool { return s1 == s2 } | ||
|
||
if !compareStrings("a", "a", true, eq) { | ||
t.Error("Expected string equality check to pass") | ||
} | ||
if !compareStrings("a", "A", true, eq) { | ||
t.Error("Expected case-insensitive string equality check to pass") | ||
} | ||
if !compareStrings(true, "true", true, eq) { | ||
t.Error("Expected boolean to string equality check to pass") | ||
} | ||
var numInt int = 1 | ||
if !compareStrings(numInt, "1", true, eq) { | ||
t.Error("Expected integer to string equality check to pass") | ||
} | ||
|
||
type StringDefinition string | ||
const ( | ||
A1 StringDefinition = "a" | ||
) | ||
if !compareStrings(A1, "a", true, eq) { | ||
t.Error("Expected string custom definition equality check to pass") | ||
} | ||
|
||
type StringAlias = string | ||
const ( | ||
A2 StringAlias = "a" | ||
) | ||
if !compareStrings(A2, "a", true, eq) { | ||
t.Error("Expected string alias equality check to pass") | ||
} | ||
} | ||
|
||
func TestNumericComparsigon(t *testing.T) { | ||
eq := func(x, y float64) bool { return x == y } | ||
|
||
var numInt int = 1 | ||
if !compareNumbers(numInt, 1, eq) { | ||
t.Error("Expected int equality check to pass") | ||
} | ||
var numUInt uint = 1 | ||
if !compareNumbers(numUInt, 1, eq) { | ||
t.Error("Expected uint equality check to pass") | ||
} | ||
var numFloat32 float32 = 1 | ||
if !compareNumbers(numFloat32, 1, eq) { | ||
t.Error("Expected float32 equality check to pass") | ||
} | ||
var numFloat64 float64 = 1 | ||
if !compareNumbers(numFloat64, 1, eq) { | ||
t.Error("Expected float64 equality check to pass") | ||
} | ||
|
||
type IntDefinition int | ||
const ( | ||
Int1 IntDefinition = 1 | ||
) | ||
if !compareNumbers(Int1, 1, eq) { | ||
t.Error("Expected int custom definition equality check to pass") | ||
} | ||
|
||
type IntAlias = int | ||
const ( | ||
Int2 IntAlias = 1 | ||
) | ||
if !compareNumbers(Int2, 1, eq) { | ||
t.Error("Expected int alias equality check to pass") | ||
} | ||
} |