Commit 9baabb1d authored by Kenton Varda's avatar Kenton Varda

Fully implement and test 'using'.

parent 08c0a048
...@@ -287,6 +287,10 @@ TEST(Encoding, UnionDefault) { ...@@ -287,6 +287,10 @@ TEST(Encoding, UnionDefault) {
} }
} }
// =======================================================================================
// Tests of generated code, not really of the encoding.
// TODO(cleanup): Move to a different test?
TEST(Encoding, NestedTypes) { TEST(Encoding, NestedTypes) {
// This is more of a test of the generated code than the encoding. // This is more of a test of the generated code than the encoding.
...@@ -310,6 +314,13 @@ TEST(Encoding, Imports) { ...@@ -310,6 +314,13 @@ TEST(Encoding, Imports) {
checkTestMessage(root.asReader().getField()); checkTestMessage(root.asReader().getField());
} }
TEST(Encoding, Using) {
MallocMessageBuilder builder;
TestUsing::Reader reader = builder.getRoot<TestUsing>().asReader();
EXPECT_EQ(TestNestedTypes::NestedEnum::BAR, reader.getOuterNestedEnum());
EXPECT_EQ(TestNestedTypes::NestedStruct::NestedEnum::QUUX, reader.getInnerNestedEnum());
}
} // namespace } // namespace
} // namespace internal } // namespace internal
} // namespace capnproto } // namespace capnproto
...@@ -56,6 +56,7 @@ using ::capnproto::test::TestEnum; ...@@ -56,6 +56,7 @@ using ::capnproto::test::TestEnum;
using ::capnproto::test::TestUnion; using ::capnproto::test::TestUnion;
using ::capnproto::test::TestUnionDefaults; using ::capnproto::test::TestUnionDefaults;
using ::capnproto::test::TestNestedTypes; using ::capnproto::test::TestNestedTypes;
using ::capnproto::test::TestUsing;
void initTestMessage(test::TestAllTypes::Builder builder); void initTestMessage(test::TestAllTypes::Builder builder);
void initTestMessage(test::TestDefaults::Builder builder); void initTestMessage(test::TestDefaults::Builder builder);
......
...@@ -266,3 +266,11 @@ struct TestNestedTypes { ...@@ -266,3 +266,11 @@ struct TestNestedTypes {
outerNestedEnum @1 :NestedEnum = bar; outerNestedEnum @1 :NestedEnum = bar;
innerNestedEnum @2 :NestedStruct.NestedEnum = quux; innerNestedEnum @2 :NestedStruct.NestedEnum = quux;
} }
struct TestUsing {
using OuterNestedEnum = TestNestedTypes.NestedEnum;
using TestNestedTypes.NestedStruct.NestedEnum;
outerNestedEnum @1 :OuterNestedEnum = bar;
innerNestedEnum @0 :NestedEnum = quux;
}
...@@ -173,9 +173,19 @@ topLine (Just statements) = liftM Left $ typeDecl statements ...@@ -173,9 +173,19 @@ topLine (Just statements) = liftM Left $ typeDecl statements
usingDecl = do usingDecl = do
usingKeyword usingKeyword
name <- located typeIdentifier maybeName <- optionMaybe $ try (do
equalsSign name <- located typeIdentifier
equalsSign
return name)
target <- declName target <- declName
name <- let
inferredName = case target of
AbsoluteName s -> return s
RelativeName s -> return s
ImportName _ -> fail "When 'using' an 'import' you must specify a name, e.g.: \
\using Foo = import \"foo.capnp\";"
MemberName _ s -> return s
in maybe inferredName return maybeName
return (UsingDecl name target) return (UsingDecl name target)
constantDecl = do constantDecl = do
...@@ -216,7 +226,7 @@ structDecl statements = do ...@@ -216,7 +226,7 @@ structDecl statements = do
return (StructDecl name annotations children) return (StructDecl name annotations children)
structLine :: Maybe [Located Statement] -> TokenParser Declaration structLine :: Maybe [Located Statement] -> TokenParser Declaration
structLine Nothing = constantDecl <|> fieldDecl <|> annotationDecl structLine Nothing = usingDecl <|> constantDecl <|> fieldDecl <|> annotationDecl
structLine (Just statements) = typeDecl statements <|> unionDecl statements <|> unionDecl statements structLine (Just statements) = typeDecl statements <|> unionDecl statements <|> unionDecl statements
unionDecl statements = do unionDecl statements = do
...@@ -272,7 +282,7 @@ interfaceDecl statements = do ...@@ -272,7 +282,7 @@ interfaceDecl statements = do
return (InterfaceDecl name annotations children) return (InterfaceDecl name annotations children)
interfaceLine :: Maybe [Located Statement] -> TokenParser Declaration interfaceLine :: Maybe [Located Statement] -> TokenParser Declaration
interfaceLine Nothing = constantDecl <|> methodDecl <|> annotationDecl interfaceLine Nothing = usingDecl <|> constantDecl <|> methodDecl <|> annotationDecl
interfaceLine (Just statements) = typeDecl statements interfaceLine (Just statements) = typeDecl statements
methodDecl = do methodDecl = do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment