v / examples / dynamic_library_loader / use_test.v
67 lines · 59 sloc · 1.98 KB · fd269cad875a864190c5cf4f46190de20ef00319
Raw
1module main
2
3import os
4import dl
5
6const vexe = os.real_path(os.getenv('VEXE'))
7const so_ext = dl.dl_ext
8
9fn test_vexe() {
10 // dump(vexe)
11 assert vexe != ''
12 // dump(os.executable())
13 // dump(@FILE)
14 // dump(cfolder)
15 // dump(so_ext)
16}
17
18fn test_can_compile_library() {
19 os.chdir(cfolder) or {}
20 library_file_path := os.join_path(cfolder, dl.get_libname('library'))
21 os.rm(library_file_path) or {}
22 v_compile('-d no_backtrace -o library -shared modules/library/library.v')
23 assert os.is_file(library_file_path)
24}
25
26fn test_can_compile_main_program() {
27 os.chdir(cfolder) or {}
28 library_file_path := os.join_path(cfolder, dl.get_libname('library'))
29 assert os.is_file(library_file_path)
30 result := v_compile('run use_shared_library.v')
31 // dump(result)
32 assert result.output.contains('hello from add_1 , num = 4')
33 assert result.output.contains('res: 4')
34 os.rm(library_file_path) or {}
35}
36
37fn test_can_compile_and_use_library_with_skip_unused_home_dir() {
38 os.chdir(cfolder) or {}
39 library_file_path := os.join_path(cfolder, dl.get_libname('library'))
40 os.rm(library_file_path) or {}
41 v_compile('-skip-unused -d no_backtrace -o library -shared modules/library/library.v')
42 assert os.is_file(library_file_path)
43 result := v_compile('run use_shared_library.v')
44 assert result.output.contains('res: 4')
45 os.rm(library_file_path) or {}
46}
47
48fn test_can_compile_and_use_library_with_skip_unused_location1_dir() {
49 os.chdir(cfolder) or {}
50 library_file_path := os.join_path(cfolder, 'location1', dl.get_libname('library'))
51 os.rm(library_file_path) or {}
52 os.mkdir('location1') or {}
53 v_compile('-skip-unused -d no_backtrace -o location1/library -shared modules/library/library.v')
54 assert os.is_file(library_file_path)
55 result := v_compile('run use_shared_library.v')
56 assert result.output.contains('res: 4')
57 os.rm(library_file_path) or {}
58}
59
60fn v_compile(vopts string) os.Result {
61 cmd := '${os.quoted_path(vexe)} -showcc ${vopts}'
62 // dump(cmd)
63 res := os.execute_or_exit(cmd)
64 // dump(res)
65 assert res.exit_code == 0
66 return res
67}
68