Skip to content

Commit

Permalink
Removed unreachable code, improved test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
asonge committed Jul 12, 2015
1 parent 7b781b0 commit b5d2511
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
9 changes: 3 additions & 6 deletions lib/graphql/lang/parser.ex
Expand Up @@ -129,9 +129,9 @@ defmodule GraphQL.Lang.Parser do
case tokens do
[{:"[",ctx}|tokens] ->
case expect(type(tokens)) do
{[{:"]",_},{:"!",ctx2}|tokens], type} -> {:ok, {:list_type, ctx, {:not_null, ctx2, type}}, tokens}
{[{:"]",_},{:"!",ctx2}|tokens], type} -> {:ok, {:not_null, ctx, {:list_type, ctx2, type}}, tokens}
{[{:"]",_}|tokens], type} -> {:ok, {:list_type, ctx, type}, tokens}
other -> other
{tokens, _} -> make_error(tokens, "end of list type")
end
[{{:identifier, name},ctx}, {:"!",ctx2}|tokens] -> {:ok, {:not_null, ctx2, {:type, ctx, name}}, tokens}
[{{:identifier, name},ctx}|tokens] -> {:ok, {:type, ctx, name}, tokens}
Expand Down Expand Up @@ -172,10 +172,7 @@ defmodule GraphQL.Lang.Parser do
defp inline_fragment([{_,ctx}|tokens]), do: make_error(tokens, "inline fragment")

defp fragment_spread([{:"...", ctx}|tokens]) do
{tokens, {:name, _, name}} = expect(case name(tokens) do
{:ok, {:name, ctx, "on"}, _} -> make_error(tokens, "not to get \"on\"")
other -> other
end)
{tokens, {:name, _, name}} = expect name(tokens)
{tokens, dirs} = optional tokens, &directives/1, []
{:ok, {:fragment_spread, ctx, [name, dirs]}, tokens}
end
Expand Down
13 changes: 11 additions & 2 deletions test/graphql_lang_parser_test.exs
Expand Up @@ -65,7 +65,10 @@ defmodule GraphqlLangParserTest do
{:query, _, [
"getUser",
{:variable_definitions, _, [
{:argument_definition, _, ["id", {:type, _, "Integer"}, nil]}
{:argument_definition, _, ["id", {:not_null, _, {:type, _, "Integer"}}, nil]},
{:argument_definition, _, ["friends", {:not_null, _, {:list_type, _, {:type, _, "Integer"}}}, nil]},
{:argument_definition, _, ["fof", {:list_type, _, {:type, _, "Integer"}}, nil]},
{:argument_definition, _, ["hidden", {:type, _, "Boolean"}, nil]}
]},
_, # directives
{:selection_set, _, [
Expand All @@ -81,7 +84,7 @@ defmodule GraphqlLangParserTest do
]}
]}
]}
]} = parse("query getUser($id: Integer) { user(id: $id) { id, name } }")
]} = parse("query getUser($id: Integer!, $friends: [Integer]!, $fof: [Integer], $hidden: Boolean) { user(id: $id) { id, name } }")
end

test "spread" do
Expand Down Expand Up @@ -122,11 +125,17 @@ defmodule GraphqlLangParserTest do
end

test "Errors" do
assert_raise GraphQL.CompileError, fn -> parse("{ 0 }") end
assert_raise GraphQL.CompileError, fn -> parse("$") end
assert_raise GraphQL.CompileError, fn -> parse("query test($id)") end
assert_raise GraphQL.CompileError, fn -> parse("query test($id: ...)") end
assert_raise GraphQL.CompileError, fn -> parse("query test($id: [Typ omg") end
assert_raise GraphQL.CompileError, fn -> parse("query $") end
assert_raise GraphQL.CompileError, fn -> parse("{ userName(id omg") end
assert_raise GraphQL.CompileError, fn -> parse("{ userName(id: {omg})}") end
assert_raise GraphQL.CompileError, fn -> parse("query test(@wrong)") end
assert_raise GraphQL.CompileError, fn -> parse("fragment on foo bar") end
assert_raise GraphQL.CompileError, fn -> parse("fragment foo bar") end
end

end
Expand Down

0 comments on commit b5d2511

Please sign in to comment.