| 1 | import toml |
| 2 | |
| 3 | // Complete text from the example in the README.md: |
| 4 | // https://github.com/toml-lang/toml/blob/3b11f6921da7b6f5db37af039aa021fee450c091/README.md#Example |
| 5 | const toml_text = '# This is a TOML document. |
| 6 | title = "TOML Example" |
| 7 | [owner] |
| 8 | name = "Tom Preston-Werner" |
| 9 | dob = 1979-05-27T07:32:00-08:00 # First class dates |
| 10 | [database] |
| 11 | server = "192.168.1.1" |
| 12 | ports = [ 8000, 8001, 8002 ] |
| 13 | connection_max = 5000 |
| 14 | enabled = true |
| 15 | [servers] |
| 16 | # Indentation (tabs and/or spaces) is allowed but not required |
| 17 | [servers.alpha] |
| 18 | ip = "10.0.0.1" |
| 19 | dc = "eqdc10" |
| 20 | [servers.beta] |
| 21 | ip = "10.0.0.2" |
| 22 | dc = "eqdc10" |
| 23 | [clients] |
| 24 | data=[["gamma","delta"],[1,2]] |
| 25 | # Line breaks are OK when inside arrays |
| 26 | hosts = [ |
| 27 | "alpha", |
| 28 | "omega" |
| 29 | ]' |
| 30 | |
| 31 | fn test_parse_compact_text() { |
| 32 | toml_doc := toml.parse_text(toml_text) or { panic(err) } |
| 33 | |
| 34 | title := toml_doc.value('title') |
| 35 | assert title == toml.Any('TOML Example') |
| 36 | assert title as string == 'TOML Example' |
| 37 | |
| 38 | database := toml_doc.value('database') as map[string]toml.Any |
| 39 | db_serv := database['server'] or { |
| 40 | panic('could not access "server" index in "database" variable') |
| 41 | } |
| 42 | assert db_serv as string == '192.168.1.1' |
| 43 | |
| 44 | assert toml_doc.value('owner.name') as string == 'Tom Preston-Werner' |
| 45 | |
| 46 | assert toml_doc.value('database.server') as string == '192.168.1.1' |
| 47 | |
| 48 | database_ports := toml_doc.value('database.ports') as []toml.Any |
| 49 | assert database_ports[0] as i64 == 8000 |
| 50 | assert database_ports[1] as i64 == 8001 |
| 51 | assert database_ports[2] as i64 == 8002 |
| 52 | assert database_ports[0].int() == 8000 |
| 53 | assert database_ports[1].int() == 8001 |
| 54 | assert database_ports[2].int() == 8002 |
| 55 | |
| 56 | assert toml_doc.value('database.connection_max') as i64 == 5000 |
| 57 | assert toml_doc.value('database.enabled') as bool == true |
| 58 | |
| 59 | assert toml_doc.value('servers.alpha.ip').string() == '10.0.0.1' |
| 60 | assert toml_doc.value('servers.alpha.dc').string() == 'eqdc10' |
| 61 | |
| 62 | assert toml_doc.value('servers.beta.ip').string() == '10.0.0.2' |
| 63 | assert toml_doc.value('servers.beta.dc').string() == 'eqdc10' |
| 64 | |
| 65 | clients_data := (toml_doc.value('clients.data') as []toml.Any) |
| 66 | // dump(clients_data) |
| 67 | // assert false |
| 68 | gamma_delta_array := clients_data[0] as []toml.Any |
| 69 | digits_array := clients_data[1] as []toml.Any |
| 70 | assert gamma_delta_array[0].string() == 'gamma' |
| 71 | assert gamma_delta_array[1].string() == 'delta' |
| 72 | assert digits_array[0].int() == 1 |
| 73 | assert digits_array[1].int() == 2 |
| 74 | |
| 75 | clients_hosts := (toml_doc.value('clients.hosts') as []toml.Any).as_strings() |
| 76 | assert clients_hosts[0] == 'alpha' |
| 77 | assert clients_hosts[1] == 'omega' |
| 78 | } |
| 79 | |