1# Check that alltypes test case works also when the .proto file defines
2# a package name.
3
4Import("env")
5
6# Build a modified alltypes.proto
7def modify_proto(target, source, env):
8    '''Add a "package test.package;" directive to the beginning of the .proto file.'''
9    data = open(str(source[0]), 'r').read()
10    open(str(target[0]), 'w').write("package test.package;\n\n" + data)
11    return 0
12
13env.Command("alltypes.proto", "#alltypes/alltypes.proto", modify_proto)
14env.Command("alltypes.options", "#alltypes/alltypes.options", Copy("$TARGET", "$SOURCE"))
15env.NanopbProto(["alltypes", "alltypes.options"])
16
17# Build a modified encode_alltypes.c
18def modify_c(target, source, env):
19    '''Add package name to type names in .c file.'''
20
21    data = open(str(source[0]), 'r').read()
22
23    type_names = ['AllTypes', 'MyEnum', 'HugeEnum']
24    for name in type_names:
25        data = data.replace(name, 'test_package_' + name)
26
27    open(str(target[0]), 'w').write(data)
28    return 0
29env.Command("encode_alltypes.c", "#alltypes/encode_alltypes.c", modify_c)
30
31# Encode and compare results to original alltypes testcase
32enc = env.Program(["encode_alltypes.c", "alltypes.pb.c", "$COMMON/pb_encode.o"])
33refdec = "$BUILD/alltypes/decode_alltypes$PROGSUFFIX"
34env.RunTest(enc)
35env.Compare(["encode_alltypes.output", "$BUILD/alltypes/encode_alltypes.output"])
36
37