1#!/bin/bash
2# By Daniele.
3#set -x
4filename=$1
5if [ [x$1] == [x] ]; then
6    echo USAGE: $0 filename.c
7    exit 4
8fi
9
10#CMOCK="../CMock/lib/cmock.rb"
11
12bname=`basename $filename`
13cat $filename |grep static|grep \( | grep \) >/tmp/$bname 
14
15if (test -f ./test/unit/modunit_$bname); then
16    echo The destination file ./test/unit/modunit_$bname already exists. Exiting...
17    exit 0
18fi
19
20cat $filename |grep "\#include " > ./test/unit/modunit_$bname
21MYSELF=`echo $bname | cut -d"." -f1`.h
22INCLUDES=`cat $filename |grep "\#include \"" |grep -v $MYSELF| cut -d '"' -f 2`
23
24echo includes are:
25echo $INCLUDES
26echo "#include \"$filename\"" >>./test/unit/modunit_$bname
27echo "#include \"check.h\"" >>./test/unit/modunit_$bname
28echo >> ./test/unit/modunit_$bname
29echo >> ./test/unit/modunit_$bname
30
31while read fn ; do
32    fname=`echo $fn | cut -d "(" -f 1| cut -d" " -f 3`
33    echo "START_TEST(tc_$fname)"               >>./test/unit/modunit_$bname 
34    echo "{"                                >>./test/unit/modunit_$bname
35    echo "   /* TODO: test this: $fn */"    >>./test/unit/modunit_$bname
36    echo "}"                                >>./test/unit/modunit_$bname
37    echo "END_TEST"                         >>./test/unit/modunit_$bname
38done </tmp/$bname
39
40echo >> ./test/unit/modunit_$bname
41echo >> ./test/unit/modunit_$bname
42echo "Suite *pico_suite(void)                       
43{
44    Suite *s = suite_create(\"PicoTCP\");             
45"  >> ./test/unit/modunit_$bname
46
47while read fn ; do
48    fname=`echo $fn | cut -d "(" -f 1| cut -d" " -f 3` 
49    echo "    TCase *TCase_$fname = tcase_create(\"Unit test for $fname\");" >> ./test/unit/modunit_$bname
50done </tmp/$bname
51
52echo >> ./test/unit/modunit_$bname
53echo >> ./test/unit/modunit_$bname
54
55while read fn ; do
56    fname=`echo $fn | cut -d "(" -f 1| cut -d" " -f 3` 
57    echo "    tcase_add_test(TCase_$fname, tc_$fname);" >> ./test/unit/modunit_$bname
58    echo "    suite_add_tcase(s, TCase_$fname);" >> ./test/unit/modunit_$bname
59done </tmp/$bname
60
61echo "return s;">> ./test/unit/modunit_$bname
62echo "}">> ./test/unit/modunit_$bname
63
64echo "                      
65int main(void)                      
66{                       
67    int fails;                      
68    Suite *s = pico_suite();                        
69    SRunner *sr = srunner_create(s);                        
70    srunner_run_all(sr, CK_NORMAL);                     
71    fails = srunner_ntests_failed(sr);                      
72    srunner_free(sr);                       
73    return fails;                       
74}" >>./test/unit/modunit_$bname
75
76
77echo Gernerated test ./test/unit/modunit_$bname
78#echo Generating mocks...
79#mkdir -p mocks
80#
81#CFILES=""
82#for i in $INCLUDES; do
83#    ii=`find -name $i | grep -v build`
84#    ruby $CMOCK $ii
85#    CFILE=`basename $ii |cut -d "." -f 1`.c
86#    CFILES="$CFILES mocks/Mock$CFILE"
87#done
88ELF=`echo build/test/modunit_$bname | sed -e "s/\.c/.elf/g"`
89
90echo 
91echo
92
93MOCKS=$(gcc -I include/ -I modules/ -I. test/unit/modunit_$bname $CFILES -lcheck -pthread -lm -lrt -o $ELF 2>&1 |grep "undefined reference to" | sed -e "s/.*\`//g" | sed -e "s/'.*$//g" |sort | uniq) 
94
95for m in $MOCKS; do
96    decl=`grep -R $m * |grep -v ");" | grep -v Binary | cut -d ":" -f 2`
97    echo $decl >> ./test/unit/modunit_$bname
98    echo "{"   >> ./test/unit/modunit_$bname
99    echo "/* TODO: MOCK ME! */">> ./test/unit/modunit_$bname
100    echo "}"   >> ./test/unit/modunit_$bname
101done
102gcc -I include/ -I modules/ -I. test/unit/modunit_$bname $CFILES -lcheck -pthread -lm -lrt -o $ELF && echo "Successfully compiled $ELF"
103
104#echo " /* TODO: MOCKS NEEDED: $MOCKS */ " >>./test/unit/modunit_$bname
105