Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#206] Test on toml-v0.4.0 example #208

Merged
merged 7 commits into from
Jul 10, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
tomland uses [PVP Versioning][1].
The changelog is available [on GitHub][2].

## 1.1.1.0
jiegillet marked this conversation as resolved.
Show resolved Hide resolved
* [#206](https://github.com/kowainik/tomland/issues/206):
Bug fix in parser and additional tests

## 1.1.0.0 — Jul 8, 2019

* [#154](https://github.com/kowainik/tomland/issues/154):
Expand Down
2 changes: 1 addition & 1 deletion src/Toml/Parser/TOML.hs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ tomlP = sc *> localTomlP Nothing <* eof

-- | Parser for a toml under a certain key
localTomlP :: Maybe Key -> Parser TOML
localTomlP key = mconcat <$> many (subArray <|> subTable <|> hasKeyP key)
localTomlP key = mconcat <$> many (subArray <|> subTable <|> (try $ hasKeyP key))
where
subTable :: Parser TOML
subTable = do
Expand Down
26 changes: 26 additions & 0 deletions test/Test/Toml/Parsing/Examples.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Test.Toml.Parsing.Examples where

import Data.Either (isRight)
import Test.Tasty.Hspec (Spec, describe, it, shouldReturn)
import Toml.Parser (parse)

import qualified Data.Text.IO as TIO

spec_Examples :: Spec
spec_Examples = describe "Can parse official TOML examples" $
mapM_ example files

example :: FilePath -> Spec
example file = it ("can parse file " ++ file) $
(isRight . parse <$> TIO.readFile ("test/examples/" ++ file))
`shouldReturn` True

files :: [FilePath]
files =
[ "fruit.toml"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose to use listDirectory function inside tests to avoid hardcoding of test cases:

You can use runIO function from hspec to run IO actions inside Spec monad:

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked into runIO when I was figuring out how to read files in a Spec but then I realized it can handle it just fine, I think I'll leave it like this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind, I guess it's necessary if I want to use listDirectory. On it.

, "example-v0.3.0.toml"
, "example-v0.4.0.toml"
, "example.toml"
, "hard_example.toml"
, "hard_example_unicode.toml"
]
8 changes: 8 additions & 0 deletions test/Test/Toml/Parsing/Unit.hs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,14 @@ tomlSpecs = do
(tomlFromTable [(makeKey ["table"], tomlFromKeyVal [str, int])])
it "can parse an empty inline TOML table" $
parseToml "table = {}" (tomlFromTable [(makeKey ["table"], mempty)])
it "can parse a table followed by an inline table" $
parseToml "[table1] \n key1 = \"some string\" \n table2 = {key2 = 123}"
(tomlFromTable [(makeKey ["table1"], tomlFromKeyVal [str])
,(makeKey ["table2"], tomlFromKeyVal [int])])
it "can parse an empty table followed by an inline table" $
parseToml "[table1] \n table2 = {key2 = 123}"
(tomlFromTable [(makeKey ["table1"], mempty)
,(makeKey ["table2"], tomlFromKeyVal [int])])
it "allows the name of the table to be any valid TOML key" $ do
parseToml "dog.\"tater.man\"={}"
(tomlFromTable [(makeKey ["dog", dquote "tater.man"], mempty)])
Expand Down
182 changes: 182 additions & 0 deletions test/examples/example-v0.3.0.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
# Comment
# I am a comment. Hear me roar. Roar.

# Table
# Tables (also known as hash tables or dictionaries) are collections of key/value pairs.
# They appear in square brackets on a line by themselves.

[Table]

key = "value" # Yeah, you can do this.

# Nested tables are denoted by table names with dots in them. Name your tables whatever crap you please, just don't use #, ., [ or ].

[dog.tater]
type = "pug"

# You don't need to specify all the super-tables if you don't want to. TOML knows how to do it for you.

# [x] you
# [x.y] don't
# [x.y.z] need these
[x.y.z.w] # for this to work

# String
# There are four ways to express strings: basic, multi-line basic, literal, and multi-line literal.
# All strings must contain only valid UTF-8 characters.

[String]
basic = "I'm a string. \"You can quote me\". Name\tJos\u00E9\nLocation\tSF."

[String.Multiline]

# The following strings are byte-for-byte equivalent:
key1 = "One\nTwo"
key2 = """One\nTwo"""
key3 = """
One
Two"""

[String.Multilined.Singleline]

# The following strings are byte-for-byte equivalent:
key1 = "The quick brown fox jumps over the lazy dog."

key2 = """
The quick brown \


fox jumps over \
the lazy dog."""

key3 = """\
The quick brown \
fox jumps over \
the lazy dog.\
"""

[String.Literal]

# What you see is what you get.
winpath = 'C:\Users\nodejs\templates'
winpath2 = '\\ServerX\admin$\system32\'
quoted = 'Tom "Dubs" Preston-Werner'
regex = '<\i\c*\s*>'


[String.Literal.Multiline]

regex2 = '''I [dw]on't need \d{2} apples'''
lines = '''
The first newline is
trimmed in raw strings.
All other whitespace
is preserved.
'''

# Integer
# Integers are whole numbers. Positive numbers may be prefixed with a plus sign.
# Negative numbers are prefixed with a minus sign.

[Integer]
key1 = +99
key2 = 42
key3 = 0
key4 = -17

# Float
# A float consists of an integer part (which may be prefixed with a plus or minus sign)
# followed by a fractional part and/or an exponent part.

[Float.fractional]

# fractional
key1 = +1.0
key2 = 3.1415
key3 = -0.01

[Float.exponent]

# exponent
key1 = 5e+22
key2 = 1e6
key3 = -2E-2

[Float.both]

# both
key = 6.626e-34

# Boolean
# Booleans are just the tokens you're used to. Always lowercase.

[Booleans]
True = true
False = false

# Datetime
# Datetimes are RFC 3339 dates.

[Datetime]
key1 = 1979-05-27T07:32:00Z
key2 = 1979-05-27T00:32:00-07:00
key3 = 1979-05-27T00:32:00.999999-07:00

# Array
# Arrays are square brackets with other primitives inside. Whitespace is ignored. Elements are separated by commas. Data types may not be mixed.

[Array]
key1 = [ 1, 2, 3 ]
key2 = [ "red", "yellow", "green" ]
key3 = [ [ 1, 2 ], [3, 4, 5] ]
key4 = [ [ 1, 2 ], ["a", "b", "c"] ] # this is ok

#Arrays can also be multiline. So in addition to ignoring whitespace, arrays also ignore newlines between the brackets.
# Terminating commas are ok before the closing bracket.

key5 = [
1, 2, 3
]
key6 = [
1,
2, # this is ok
]

# Array of Tables
# These can be expressed by using a table name in double brackets.
# Each table with the same double bracketed name will be an element in the array.
# The tables are inserted in the order encountered.

[[products]]
name = "Hammer"
sku = 738594937

[[products]]

[[products]]
name = "Nail"
sku = 284758393
color = "gray"


# You can create nested arrays of tables as well.

[[fruit]]
name = "apple"

[fruit.physical]
color = "red"
shape = "round"

[[fruit.variety]]
name = "red delicious"

[[fruit.variety]]
name = "granny smith"

[[fruit]]
name = "banana"

[[fruit.variety]]
name = "plantain"

Loading