miantalha45 commented on code in PR #3394:
URL: https://github.com/apache/fory/pull/3394#discussion_r3032176495


##########
compiler/fory_compiler/tests/test_javascript_codegen.py:
##########
@@ -0,0 +1,436 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+"""Tests for JavaScript code generation."""
+
+from pathlib import Path
+from textwrap import dedent
+
+from fory_compiler.frontend.fdl.lexer import Lexer
+from fory_compiler.frontend.fdl.parser import Parser
+from fory_compiler.generators.base import GeneratorOptions
+from fory_compiler.generators.javascript import JavaScriptGenerator
+from fory_compiler.ir.ast import Schema
+
+
+def parse_fdl(source: str) -> Schema:
+    return Parser(Lexer(source).tokenize()).parse()
+
+
+def generate_javascript(source: str) -> str:
+    schema = parse_fdl(source)
+    options = GeneratorOptions(output_dir=Path("/tmp"))
+    generator = JavaScriptGenerator(schema, options)
+    files = generator.generate()
+    assert len(files) == 1, f"Expected 1 file, got {len(files)}"
+    return files[0].content
+
+
+def test_javascript_enum_generation():
+    """Test that enums are properly generated."""
+    source = dedent(
+        """
+        package example;
+
+        enum Color [id=101] {
+            RED = 0;
+            GREEN = 1;
+            BLUE = 2;
+        }
+        """
+    )
+    output = generate_javascript(source)
+
+    # Check enum definition
+    assert "export enum color" in output
+    assert "RED = 0" in output
+    assert "GREEN = 1" in output
+    assert "BLUE = 2" in output
+    assert "Type ID 101" in output
+
+
+def test_javascript_message_generation():
+    """Test that messages are properly generated as interfaces."""
+    source = dedent(
+        """
+        package example;
+
+        message Person [id=102] {
+            string name = 1;
+            int32 age = 2;
+            optional string email = 3;
+        }
+        """
+    )
+    output = generate_javascript(source)
+
+    # Check interface definition
+    assert "export interface person" in output
+    assert "name: string;" in output
+    assert "age: number;" in output
+    assert "email?: string | undefined;" in output
+    assert "Type ID 102" in output
+
+
+def test_javascript_nested_message():
+    """Test that nested messages are properly generated."""
+    source = dedent(
+        """
+        package example;
+
+        message Person [id=100] {
+            string name = 1;
+
+            message Address [id=101] {
+                string street = 1;
+                string city = 2;
+            }
+
+            Address address = 2;
+        }
+        """
+    )
+    output = generate_javascript(source)
+
+    # Check nested interface
+    assert "export interface person" in output
+    assert "export namespace person" in output
+    assert "export interface address" in output
+    assert "street: string;" in output
+    assert "city: string;" in output
+
+
+def test_javascript_nested_enum():
+    """Test that nested enums are properly generated."""
+    source = dedent(
+        """
+        package example;
+
+        message Person [id=100] {
+            string name = 1;
+
+            enum PhoneType [id=101] {
+                MOBILE = 0;
+                HOME = 1;
+            }
+        }
+        """
+    )
+    output = generate_javascript(source)
+
+    # Check nested enum
+    assert "export namespace person" in output
+    assert "export enum phoneType" in output

Review Comment:
   generated enum is phoneType instead of PhoneType because i try to follow the 
same naming conventions as used in JavaScript file names and also in main 
runtime.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to