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