github.com/Konstantin8105/c4go@v0.0.0-20240505174241-768bb1c65a51/preprocessor/parse_comments_test.go (about)

     1  package preprocessor
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  )
     7  
     8  func TestParseComments(t *testing.T) {
     9  	testCases := []struct {
    10  		ent      entity
    11  		code     []string
    12  		comments []Comment
    13  	}{
    14  		{
    15  			ent: entity{positionInSource: 10, include: "file.c"},
    16  			code: []string{
    17  				"NULL",
    18  				"// comment1",
    19  			},
    20  			comments: []Comment{
    21  				{
    22  					File:    "file.c",
    23  					Line:    10,
    24  					Comment: "// comment1",
    25  				},
    26  			},
    27  		},
    28  		{
    29  			ent: entity{positionInSource: 10, include: "file.c"},
    30  			code: []string{
    31  				"NULL",
    32  				"/* comment1 */",
    33  			},
    34  			comments: []Comment{
    35  				{
    36  					File:    "file.c",
    37  					Line:    10,
    38  					Comment: "// comment1 ",
    39  				},
    40  			},
    41  		},
    42  		{
    43  			ent: entity{positionInSource: 10, include: "file.c"},
    44  			code: []string{
    45  				"NULL",
    46  				"// comment1",
    47  				"/* comment2 */",
    48  			},
    49  			comments: []Comment{
    50  				{
    51  					File:    "file.c",
    52  					Line:    10,
    53  					Comment: "// comment1",
    54  				},
    55  				{
    56  					File:    "file.c",
    57  					Line:    11,
    58  					Comment: "// comment2 ",
    59  				},
    60  			},
    61  		},
    62  		{
    63  			ent: entity{positionInSource: 10, include: "file.c"},
    64  			code: []string{
    65  				"NULL",
    66  				"/* comment2 */",
    67  				"// comment1",
    68  			},
    69  			comments: []Comment{
    70  				{
    71  					File:    "file.c",
    72  					Line:    10,
    73  					Comment: "// comment2 ",
    74  				},
    75  				{
    76  					File:    "file.c",
    77  					Line:    11,
    78  					Comment: "// comment1",
    79  				},
    80  			},
    81  		},
    82  		{
    83  			ent: entity{positionInSource: 10, include: "file.c"},
    84  			code: []string{
    85  				"NULL",
    86  				"/* comment */ // comment1",
    87  			},
    88  			comments: []Comment{
    89  				{
    90  					File:    "file.c",
    91  					Line:    10,
    92  					Comment: "// comment ",
    93  				},
    94  				{
    95  					File:    "file.c",
    96  					Line:    10,
    97  					Comment: "// comment1",
    98  				},
    99  			},
   100  		},
   101  		{
   102  			ent: entity{positionInSource: 10, include: "file.c"},
   103  			code: []string{
   104  				"NULL",
   105  				"// comment1 /* comment */",
   106  			},
   107  			comments: []Comment{
   108  				{
   109  					File:    "file.c",
   110  					Line:    10,
   111  					Comment: "// comment1 /* comment */",
   112  				},
   113  			},
   114  		},
   115  		{
   116  			ent: entity{positionInSource: 10, include: "file.c"},
   117  			code: []string{
   118  				"NULL",
   119  				"/* Text1",
   120  				"Text2",
   121  				"Text3 */",
   122  			},
   123  			comments: []Comment{
   124  				{
   125  					File:    "file.c",
   126  					Line:    10,
   127  					Comment: "// Text1",
   128  				},
   129  				{
   130  					File:    "file.c",
   131  					Line:    10,
   132  					Comment: "//Text2",
   133  				},
   134  				{
   135  					File:    "file.c",
   136  					Line:    10,
   137  					Comment: "//Text3 ",
   138  				},
   139  			},
   140  		},
   141  		{
   142  			ent: entity{positionInSource: 10, include: "file.c"},
   143  			code: []string{
   144  				"NULL",
   145  				"/* Text-1 */ // Text 0",
   146  				"/* Text1",
   147  				"Text2",
   148  				"Text3 */ // Text 4",
   149  				"// Text 5",
   150  			},
   151  			comments: []Comment{
   152  				{
   153  					File:    "file.c",
   154  					Line:    10,
   155  					Comment: "// Text-1 ",
   156  				},
   157  				{
   158  					File:    "file.c",
   159  					Line:    10,
   160  					Comment: "// Text 0",
   161  				},
   162  				{
   163  					File:    "file.c",
   164  					Line:    11,
   165  					Comment: "// Text1",
   166  				},
   167  				{
   168  					File:    "file.c",
   169  					Line:    11,
   170  					Comment: "//Text2",
   171  				},
   172  				{
   173  					File:    "file.c",
   174  					Line:    11,
   175  					Comment: "//Text3 ",
   176  				},
   177  				{
   178  					File:    "file.c",
   179  					Line:    13,
   180  					Comment: "// Text 4",
   181  				},
   182  				{
   183  					File:    "file.c",
   184  					Line:    14,
   185  					Comment: "// Text 5",
   186  				},
   187  			},
   188  		},
   189  	}
   190  	for i := range testCases {
   191  		t.Run(fmt.Sprintf("Test:%d", i), func(t *testing.T) {
   192  			var result []Comment
   193  			for j := range testCases[i].code {
   194  				testCases[i].ent.lines = append(testCases[i].ent.lines,
   195  					&testCases[i].code[j])
   196  			}
   197  			testCases[i].ent.parseComments(&result)
   198  			if len(result) != len(testCases[i].comments) {
   199  				t.Fatalf("Size of comments is not same\nresult = '%d'\nexpect = '%d'",
   200  					len(result),
   201  					len(testCases[i].comments))
   202  			}
   203  			for j := range result {
   204  				if result[j].File != testCases[i].comments[j].File {
   205  					t.Fatalf("File is not same")
   206  				}
   207  				if result[j].Comment != testCases[i].comments[j].Comment {
   208  					t.Fatalf("Comment is not same.\nresult = '%s'\nexpect = '%s'",
   209  						result[j].Comment,
   210  						testCases[i].comments[j].Comment)
   211  				}
   212  				if result[j].Line != testCases[i].comments[j].Line {
   213  					t.Fatalf("Lines is not same\nresult = '%d'\nexpect = '%d'",
   214  						result[j].Line,
   215  						testCases[i].comments[j].Line)
   216  				}
   217  			}
   218  		})
   219  	}
   220  }