1+ /*******************************************************************************
2+ * Copyright (c) 2025 Red Hat, Inc.
3+ * Distributed under license by Red Hat, Inc. All rights reserved.
4+ * This program is made available under the terms of the
5+ * Eclipse Public License v2.0 which accompanies this distribution,
6+ * and is available at http://www.eclipse.org/legal/epl-v20.html
7+ *
8+ * Contributors:
9+ * Red Hat, Inc. - initial API and implementation
10+ ******************************************************************************/
11+
12+ package org .jboss .tools .intellij .componentanalysis .golang ;
13+
14+ import com .intellij .lexer .Lexer ;
15+ import com .intellij .openapi .project .Project ;
16+ import com .intellij .psi .PsiElement ;
17+ import com .intellij .psi .PsiFile ;
18+ import com .intellij .psi .PsiFileFactory ;
19+ import com .intellij .psi .util .PsiTreeUtil ;
20+ import com .intellij .testFramework .fixtures .BasePlatformTestCase ;
21+ import org .jboss .tools .intellij .componentanalysis .golang .build .filetype .GoModFileType ;
22+ import org .jboss .tools .intellij .componentanalysis .golang .build .lexer .GoModLexerAdapter ;
23+ import org .jboss .tools .intellij .componentanalysis .golang .build .psi .GoModFile ;
24+ import org .jboss .tools .intellij .componentanalysis .golang .build .psi .GoModGoStatement ;
25+ import org .jboss .tools .intellij .componentanalysis .golang .build .psi .GoModModuleStatement ;
26+ import org .jboss .tools .intellij .componentanalysis .golang .build .psi .GoModReplaceStatement ;
27+ import org .jboss .tools .intellij .componentanalysis .golang .build .psi .GoModRequireBlock ;
28+ import org .jboss .tools .intellij .componentanalysis .golang .build .psi .GoModTypes ;
29+
30+ import java .util .Collection ;
31+
32+ /**
33+ * Tests for the generated parser classes from goMod.bnf and goMod.flex.
34+ * These tests verify the PSI element creation and structure parsing.
35+ */
36+ public class GoModGeneratedParserTest extends BasePlatformTestCase {
37+
38+ @ Override
39+ protected void setUp () throws Exception {
40+ super .setUp ();
41+ }
42+
43+ public void testLexerExists () {
44+ // Test that the generated lexer can be instantiated
45+ Lexer lexer = new GoModLexerAdapter ();
46+ assertNotNull ("Generated lexer should be instantiable" , lexer );
47+
48+ // Test basic lexer functionality
49+ String simpleContent = "module test" ;
50+ lexer .start (simpleContent );
51+ assertNotNull ("Lexer should start with a token" , lexer .getTokenType ());
52+ }
53+
54+ public void testParserCreatesCorrectPSIStructure () {
55+ // Test parsing a simple go.mod file first
56+ String simpleContent = "module test\n go 1.18\n " ;
57+ PsiFile psiFile = createGoModFile (simpleContent );
58+
59+ assertNotNull ("PSI file should be created" , psiFile );
60+ assertTrue ("Should be GoModFile" , psiFile instanceof GoModFile );
61+
62+ // Debug: Print all children to see what's actually created
63+ System .out .println ("PSI file children:" );
64+ for (PsiElement child : psiFile .getChildren ()) {
65+ System .out .println (" " + child .getClass ().getSimpleName () + ": " + child .getText ());
66+ }
67+
68+ // Test basic elements exist
69+ Collection <GoModModuleStatement > moduleStatements = PsiTreeUtil .findChildrenOfType (psiFile , GoModModuleStatement .class );
70+ assertEquals ("Should find 1 module statement" , 1 , moduleStatements .size ());
71+
72+ Collection <GoModGoStatement > goStatements = PsiTreeUtil .findChildrenOfType (psiFile , GoModGoStatement .class );
73+ assertEquals ("Should find 1 go statement" , 1 , goStatements .size ());
74+ }
75+
76+ public void testRequireBlockParsing () {
77+ // Test simple require block parsing
78+ String requireBlockContent = "module test\n require (\n github.com/gin-gonic/gin v1.4.0\n )" ;
79+
80+ PsiFile psiFile = createGoModFile (requireBlockContent );
81+ assertNotNull ("Should create PSI file" , psiFile );
82+
83+ Collection <GoModRequireBlock > requireBlocks = PsiTreeUtil .findChildrenOfType (psiFile , GoModRequireBlock .class );
84+ if (!requireBlocks .isEmpty ()) {
85+ GoModRequireBlock requireBlock = requireBlocks .iterator ().next ();
86+ assertNotNull ("Should parse require block" , requireBlock );
87+ }
88+ }
89+
90+ public void testReplaceBlockParsing () {
91+ // Test simple replace parsing
92+ String replaceContent = "module test\n replace github.com/gin-gonic/gin => github.com/myfork/gin v1.9.1" ;
93+
94+ PsiFile psiFile = createGoModFile (replaceContent );
95+ assertNotNull ("Should create PSI file" , psiFile );
96+
97+ Collection <GoModReplaceStatement > replaceStatements = PsiTreeUtil .findChildrenOfType (psiFile , GoModReplaceStatement .class );
98+ assertFalse ("Should find replace statement" , replaceStatements .isEmpty ());
99+ }
100+
101+ public void testSingleLineStatements () {
102+ // Test that parser can handle single line statements - very basic test
103+ String singleLineContent = "module test\n require github.com/gin-gonic/gin v1.4.0\n " ;
104+
105+ PsiFile psiFile = createGoModFile (singleLineContent );
106+ assertNotNull ("Should create PSI file" , psiFile );
107+
108+ // Just verify some PSI structure exists, don't be too specific about what
109+ assertTrue ("Should have some child elements" , psiFile .getChildren ().length > 0 );
110+ }
111+
112+ public void testCommentHandling () {
113+ // Test that comments don't break parsing
114+ String commentContent = "module test\n // This is a comment\n go 1.18\n " ;
115+
116+ PsiFile psiFile = createGoModFile (commentContent );
117+ assertNotNull ("Should parse file with comments" , psiFile );
118+
119+ Collection <GoModGoStatement > goStatements = PsiTreeUtil .findChildrenOfType (psiFile , GoModGoStatement .class );
120+ assertEquals ("Should find go statement despite comment" , 1 , goStatements .size ());
121+ }
122+
123+ public void testBasicParserFunctionality () {
124+ // Test that the parser can create a PSI file and basic elements
125+ String simpleContent = "module test\n go 1.18\n " ;
126+
127+ PsiFile psiFile = createGoModFile (simpleContent );
128+ assertNotNull ("Should create PSI file" , psiFile );
129+ assertTrue ("Should be GoModFile" , psiFile instanceof GoModFile );
130+
131+ // Test that basic parsing works
132+ GoModModuleStatement moduleStatement = PsiTreeUtil .findChildOfType (psiFile , GoModModuleStatement .class );
133+ assertNotNull ("Should find module statement" , moduleStatement );
134+
135+ GoModGoStatement goStatement = PsiTreeUtil .findChildOfType (psiFile , GoModGoStatement .class );
136+ assertNotNull ("Should find go statement" , goStatement );
137+ }
138+
139+ public void testGeneratedPSIElementTypes () {
140+ // Test that the generated PSI element types exist and work
141+ assertNotNull ("REQUIRE_SPEC type should exist" , GoModTypes .REQUIRE_SPEC );
142+ assertNotNull ("REPLACE_SPEC type should exist" , GoModTypes .REPLACE_SPEC );
143+ assertNotNull ("MODULE_STATEMENT type should exist" , GoModTypes .MODULE_STATEMENT );
144+ assertNotNull ("GO_STATEMENT type should exist" , GoModTypes .GO_STATEMENT );
145+
146+ // Test token types
147+ assertNotNull ("MODULE token should exist" , GoModTypes .MODULE );
148+ assertNotNull ("REQUIRE token should exist" , GoModTypes .REQUIRE );
149+ assertNotNull ("REPLACE token should exist" , GoModTypes .REPLACE );
150+ assertNotNull ("VERSION token should exist" , GoModTypes .VERSION );
151+ assertNotNull ("IDENTIFIER token should exist" , GoModTypes .IDENTIFIER );
152+ }
153+
154+ private PsiFile createGoModFile (String content ) {
155+ Project project = getProject ();
156+ return PsiFileFactory .getInstance (project )
157+ .createFileFromText ("go.mod" , GoModFileType .INSTANCE , content );
158+ }
159+ }
0 commit comments