|
| 1 | +const jsonToGo = require("./json-to-go"); |
| 2 | + |
| 3 | +function quote(str) { |
| 4 | + return "'" + str |
| 5 | + .replace(/\t/g, "\\t") |
| 6 | + .replace(/\n/g, "\\n") |
| 7 | + .replace(/'/g, "\\'") + "'" |
| 8 | +} |
| 9 | + |
| 10 | +function test() { |
| 11 | + const testCases = [ |
| 12 | + { |
| 13 | + input: '{"SourceCode": ""}', |
| 14 | + expected: |
| 15 | + 'type AutoGenerated struct {\n\tSourceCode string `json:"SourceCode"`\n}' |
| 16 | + }, |
| 17 | + { |
| 18 | + input: '{"source_code": ""}', |
| 19 | + expected: |
| 20 | + 'type AutoGenerated struct {\n\tSourceCode string `json:"source_code"`\n}' |
| 21 | + }, |
| 22 | + { |
| 23 | + input: '{"sourceCode": ""}', |
| 24 | + expected: |
| 25 | + 'type AutoGenerated struct {\n\tSourceCode string `json:"sourceCode"`\n}' |
| 26 | + }, |
| 27 | + { |
| 28 | + input: '{"SOURCE_CODE": ""}', |
| 29 | + expected: |
| 30 | + 'type AutoGenerated struct {\n\tSourceCode string `json:"SOURCE_CODE"`\n}' |
| 31 | + }, |
| 32 | + { |
| 33 | + input: '{"PublicIP": ""}', |
| 34 | + expected: |
| 35 | + 'type AutoGenerated struct {\n\tPublicIP string `json:"PublicIP"`\n}' |
| 36 | + }, |
| 37 | + { |
| 38 | + input: '{"public_ip": ""}', |
| 39 | + expected: |
| 40 | + 'type AutoGenerated struct {\n\tPublicIP string `json:"public_ip"`\n}' |
| 41 | + }, |
| 42 | + { |
| 43 | + input: '{"publicIP": ""}', |
| 44 | + expected: |
| 45 | + 'type AutoGenerated struct {\n\tPublicIP string `json:"publicIP"`\n}' |
| 46 | + }, |
| 47 | + { |
| 48 | + input: '{"PUBLIC_IP": ""}', |
| 49 | + expected: |
| 50 | + 'type AutoGenerated struct {\n\tPublicIP string `json:"PUBLIC_IP"`\n}' |
| 51 | + } |
| 52 | + ]; |
| 53 | + |
| 54 | + for (const testCase of testCases) { |
| 55 | + const got = jsonToGo(testCase.input); |
| 56 | + if (got.error) { |
| 57 | + console.assert(!got.error, `format('${testCase.input}'): ${got.error}`); |
| 58 | + } else { |
| 59 | + console.assert( |
| 60 | + got.go === testCase.expected, |
| 61 | + `format('${testCase.input}'): \n\tgot: ${quote(got.go)}\n\twant: ${ |
| 62 | + quote(testCase.expected) |
| 63 | + }` |
| 64 | + ); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + console.log("done") |
| 69 | +} |
| 70 | + |
| 71 | +test(); |
0 commit comments