From 85c5103ba3932ec8a73a268de6de1b4f7d177b57 Mon Sep 17 00:00:00 2001 From: Alexey Date: Sun, 19 Apr 2020 00:45:17 +0300 Subject: [PATCH] tests: test cflags parsing --- vlib/v/table/cflags.v | 2 +- vlib/v/table/cflags_test.v | 75 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 vlib/v/table/cflags_test.v diff --git a/vlib/v/table/cflags.v b/vlib/v/table/cflags.v index 476e7a5dc4..389bf9bf83 100644 --- a/vlib/v/table/cflags.v +++ b/vlib/v/table/cflags.v @@ -22,7 +22,7 @@ pub fn (var table Table) parse_cflag(cflg, mod string, ctimedefines []string) ?b flag_orig := cflg.trim_space() var flag := flag_orig if flag == '' { - return true + return none } var fos := '' var allowed_os_overrides := ['linux', 'darwin', 'freebsd', 'windows', 'mingw', 'solaris'] diff --git a/vlib/v/table/cflags_test.v b/vlib/v/table/cflags_test.v new file mode 100644 index 0000000000..892e8acc69 --- /dev/null +++ b/vlib/v/table/cflags_test.v @@ -0,0 +1,75 @@ +import table +import v.cflag + +const ( + module_name = 'main' + cdefines = []string + no_name = '' + no_flag = '' + no_os = '' +) + +fn test_parse_valid_cflags() { + t := table.new_table() + expected_flags := [ + make_flag('freebsd', '-I', '/usr/local/include/freetype2'), + make_flag('linux', '-l', 'glfw'), + make_flag('mingw', no_name, '-mwindows'), + make_flag('solaris', '-L', '/opt/local/lib'), + make_flag('darwin', '-framework', 'Cocoa'), + make_flag('windows', '-l', 'gdi32'), + make_flag(no_os, '-l', 'mysqlclient'), + make_flag(no_os, no_name, '-test'), + ] + parse_valid_flag(t, '-lmysqlclient') + parse_valid_flag(t, '-test') + parse_valid_flag(t, 'darwin -framework Cocoa') + parse_valid_flag(t, 'freebsd -I/usr/local/include/freetype2') + parse_valid_flag(t, 'linux -lglfw') + parse_valid_flag(t, 'mingw -mwindows') + parse_valid_flag(t, 'solaris -L/opt/local/lib') + parse_valid_flag(t, 'windows -lgdi32') + assert t.cflags.len == expected_flags.len + for f in expected_flags { + assert t.has_cflag(f) + } +} + +fn test_parse_invalid_cflags() { + t := table.new_table() + // -I, -L, -l must have values + assert_parse_invalid_flag(t, 'windows -l') + assert_parse_invalid_flag(t, '-I') + assert_parse_invalid_flag(t, '-L') + // OS/compiler name only is not allowed + assert_parse_invalid_flag(t, 'darwin') + assert_parse_invalid_flag(t, 'freebsd') + assert_parse_invalid_flag(t, 'linux') + assert_parse_invalid_flag(t, 'mingw') + assert_parse_invalid_flag(t, 'solaris') + assert_parse_invalid_flag(t, 'windows') + // Empty flag is not allowed + assert_parse_invalid_flag(t, no_flag) + assert t.cflags.len == 0 +} + +fn parse_valid_flag(t &table.Table, flag string) { + t.parse_cflag(flag, module_name, cdefines) or { + } +} + +fn assert_parse_invalid_flag(t &table.Table, flag string) { + t.parse_cflag(flag, module_name, cdefines) or { + return + } + assert false +} + +fn make_flag(os, name, value string) cflag.CFlag { + return cflag.CFlag{ + mod: module_name + os: os + name: name + value: value + } +}