Skip to content

Commit

Permalink
Updating and improving test for CamelCaseKeys
Browse files Browse the repository at this point in the history
  • Loading branch information
Rican7 committed Jan 16, 2019
1 parent a8edf50 commit 46137a2
Showing 1 changed file with 43 additions and 11 deletions.
54 changes: 43 additions & 11 deletions transform/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ func TestCamelCaseKeys(t *testing.T) {
"camelCaseKey": "a camelCase key",
"UpperCamelCaseKey": "an UpperCamelCase key",
"a-strange_mixedAndConfused_CaseStyle": "an UpperCamelCase key",
"aKeyWithRepeatedCapitalLettersLikeURL": "a key with repeated capitals"
"aKeyWithRepeatedCapitalLettersLikeURL": "a key with repeated capitals",
"aKeyWithRepeatedCapitalLettersLikeURLButNotAtTheEnd": "a key with repeated capitals"
}
`

Expand All @@ -236,23 +237,54 @@ func TestCamelCaseKeys(t *testing.T) {
"camelCaseKey": "a camelCase key",
"upperCamelCaseKey": "an UpperCamelCase key",
"aStrangeMixedAndConfusedCaseStyle": "an UpperCamelCase key",
"aKeyWithRepeatedCapitalLettersLikeUrl": "a key with repeated capitals"
"aKeyWithRepeatedCapitalLettersLikeURL": "a key with repeated capitals",
"aKeyWithRepeatedCapitalLettersLikeURLButNotAtTheEnd": "a key with repeated capitals"
}
`

trans := CamelCaseKeys()
const lowerRepeatedCapsCamelCaseJSON = `
{
"title": "Example Title",
"description": "whatever",
"createdAt": "2015-11-17T20:43:31.0463576-05:00",
"updatedAt": "2018-12-24T13:21:15.7883416-07:00",
"isActive": true,
"imageUrl": "https://example.com/image.png",
"timeToLive": 600,
"$weirdKey" : "with-colon-spacing-before-value",
"plan9Key" : "just an example with a numeral",
"__metadataKey": "You see this in OData 2.0",
"__metadata": "You see this in OData 2.0",
"capitalized": "a capitalized key",
"aHyphenatedKey": "a hyphenated key",
"camelCaseKey": "a camelCase key",
"upperCamelCaseKey": "an UpperCamelCase key",
"aStrangeMixedAndConfusedCaseStyle": "an UpperCamelCase key",
"aKeyWithRepeatedCapitalLettersLikeUrl": "a key with repeated capitals",
"aKeyWithRepeatedCapitalLettersLikeUrlButNotAtTheEnd": "a key with repeated capitals"
}
`

camelCaseTrans := CamelCaseKeys(false)
lowerRepeatedCapsCamelCaseTrans := CamelCaseKeys(true)

for _, testCase := range []struct {
jsonBytes string
direction Direction
trans Transformer
inputJSONBytes string
direction Direction
expectedOutput string
}{
{originalJSON, Marshal},
{originalJSON, Unmarshal},
{camelCaseJSON, Marshal},
{camelCaseJSON, Unmarshal},
{camelCaseTrans, originalJSON, Marshal, camelCaseJSON},
{camelCaseTrans, originalJSON, Unmarshal, camelCaseJSON},
{camelCaseTrans, camelCaseJSON, Marshal, camelCaseJSON},
{camelCaseTrans, camelCaseJSON, Unmarshal, camelCaseJSON},
{lowerRepeatedCapsCamelCaseTrans, originalJSON, Marshal, lowerRepeatedCapsCamelCaseJSON},
{lowerRepeatedCapsCamelCaseTrans, originalJSON, Unmarshal, lowerRepeatedCapsCamelCaseJSON},
{lowerRepeatedCapsCamelCaseTrans, camelCaseJSON, Marshal, lowerRepeatedCapsCamelCaseJSON},
{lowerRepeatedCapsCamelCaseTrans, camelCaseJSON, Unmarshal, lowerRepeatedCapsCamelCaseJSON},
} {
if output := trans([]byte(testCase.jsonBytes), testCase.direction); string(output) != camelCaseJSON {
t.Errorf("%s output of %s doesn't match expected %s", testCase.direction, output, camelCaseJSON)
if output := testCase.trans([]byte(testCase.inputJSONBytes), testCase.direction); string(output) != testCase.expectedOutput {
t.Errorf("%s output of %s doesn't match expected %s", testCase.direction, output, testCase.expectedOutput)
}
}
}
Expand Down

0 comments on commit 46137a2

Please sign in to comment.