|
1 | 1 | package config |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "io/ioutil" |
5 | 6 | "os" |
6 | 7 | "path/filepath" |
@@ -34,41 +35,49 @@ func TestReadingWritingConfig(t *testing.T) { |
34 | 35 | filename := Filename(tmpDir) |
35 | 36 | assert.NoError(t, err) |
36 | 37 |
|
37 | | - currentConfig := Config{ |
38 | | - GithubUsername: "user", |
39 | | - APIKey: "MyKey", |
40 | | - ExercismDirectory: "/exercism/directory", |
41 | | - Hostname: "localhost\r\n", |
42 | | - } |
43 | | - sanitizedConfig := Config{ |
| 38 | + writtenConfig := Config{ |
44 | 39 | GithubUsername: "user", |
45 | 40 | APIKey: "MyKey", |
46 | 41 | ExercismDirectory: "/exercism/directory", |
47 | 42 | Hostname: "localhost", |
48 | 43 | } |
49 | 44 |
|
50 | | - ToFile(filename, currentConfig) |
| 45 | + ToFile(filename, writtenConfig) |
51 | 46 |
|
52 | 47 | loadedConfig, err := FromFile(filename) |
53 | 48 | assert.NoError(t, err) |
54 | 49 |
|
55 | | - assert.Equal(t, sanitizedConfig, loadedConfig) |
| 50 | + assert.Equal(t, writtenConfig, loadedConfig) |
56 | 51 | } |
57 | 52 |
|
58 | | -func TestSanitizeFields(t *testing.T) { |
59 | | - config := Config{ |
60 | | - GithubUsername: "user ", |
61 | | - APIKey: "MyKey ", |
62 | | - ExercismDirectory: "/home/user name\r\n", |
63 | | - Hostname: "localhost\n", |
64 | | - } |
| 53 | +func TestDecodingConfig(t *testing.T) { |
| 54 | + unsanitizedJson := `{"githubUsername":"user ","apiKey":"MyKey ","exercismDirectory":"/exercism/directory\r\n","hostname":"localhost \r\n"}` |
65 | 55 | sanitizedConfig := Config{ |
66 | 56 | GithubUsername: "user", |
67 | 57 | APIKey: "MyKey", |
68 | | - ExercismDirectory: "/home/user name", |
| 58 | + ExercismDirectory: "/exercism/directory", |
69 | 59 | Hostname: "localhost", |
70 | 60 | } |
71 | | - sanitize(&config) |
| 61 | + b := bytes.NewBufferString(unsanitizedJson) |
| 62 | + c, err := Decode(b) |
72 | 63 |
|
73 | | - assert.Equal(t, config, sanitizedConfig) |
| 64 | + assert.NoError(t, err) |
| 65 | + assert.Equal(t, sanitizedConfig, c) |
| 66 | +} |
| 67 | + |
| 68 | +func TestEncodingConfig(t *testing.T) { |
| 69 | + currentConfig := Config{ |
| 70 | + GithubUsername: "user\r\n", |
| 71 | + APIKey: "MyKey ", |
| 72 | + ExercismDirectory: "/home/user name ", |
| 73 | + Hostname: "localhost ", |
| 74 | + } |
| 75 | + sanitizedJson := `{"githubUsername":"user","apiKey":"MyKey","exercismDirectory":"/home/user name","hostname":"localhost"} |
| 76 | +` |
| 77 | + |
| 78 | + buf := new(bytes.Buffer) |
| 79 | + err := Encode(buf, currentConfig) |
| 80 | + |
| 81 | + assert.NoError(t, err) |
| 82 | + assert.Equal(t, sanitizedJson, buf.String()) |
74 | 83 | } |
0 commit comments