<commit_before>defmodule ElixirScript.Translator.Bug.Test do
  use ExUnit.Case
  import ElixirScript.TestHelper

  test "Translate function with 0 arguments" do
    ex_ast = quote do
        def test do
          :atom
        end
    end

    js_code = """
      const test = Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([],function(){
        return Symbol.for('atom');
      }));
    """

    assert_translation(ex_ast, js_code)
  end

  test "Translate react element" do
    ex_ast = quote do
      React.createElement(
        React.Text,
        %{"style" => styles().welcome},
        "Welcome to React Native!"
      )
    end

    js_code = """
     React.createElement(React.Text,Object.freeze({
             style: Bootstrap.Core.Functions.call_property(styles,'welcome')
       }),'Welcome to React Native!')
    """

    assert_translation(ex_ast, js_code)

  end

  test "correctly not create 2 imports" do
    ex_ast = quote do
      defmodule App.Todo do
        JS.import JQuery, "jquery"
        JQuery.(e.target)
      end
    end

    js_code = """
    JQuery(Bootstrap.Core.Functions.call_property(e,'target'));
    """

    assert_translation(ex_ast, js_code)
  end

  test "correctly translate module names when used" do
    ex_ast = quote do
      @graphic_store App.Stores.GraphicStore.create_store()
    end

    js_code = """
      const graphic_store = Bootstrap.Core.Functions.call_property(App.Stores.GraphicStore, 'create_store');

    """

    assert_translation(ex_ast, js_code)
  end

  test "replace !" do
    ex_ast = quote do
      Bootstrap.Enum.fetch!(data, i)
    end

    js_code = """
      Bootstrap.Enum.fetch__emark__(data, i)
    """

    assert_translation(ex_ast, js_code)
  end

  test "chain calls correctly" do
    ex_ast = quote do
      :this.getRawCanvas().getContext("2d")
    end

    js_code = """
      Bootstrap.Core.Functions.call_property(this, 'getRawCanvas').getContext('2d')
    """

    assert_translation(ex_ast, js_code)


    ex_ast = quote do
      :this.getRawCanvas(one).get("fg").getContext("2d")
    end

    js_code = """
      this.getRawCanvas(one).get('fg').getContext('2d')
    """

    assert_translation(ex_ast, js_code)
  end

  test "correctly call multi-module functions" do
    ex_ast = quote do
      def getDispatcher() do
        DeLorean.Flux.createDispatcher(%{
          startPainting: fn() -> this.dispatch("startPainting") end,
          stopPainting: fn() -> this.dispatch("stopPainting") end,
          addPoint: fn(data) -> this.dispatch("addPoint", data) end,
          getStores: fn() -> %{ graphic: GraphicStore } end
        })
      end
    end


    js_code = """
     const getDispatcher = Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([],function()    {
             return     DeLorean.Flux.createDispatcher(Object.freeze({
             [Symbol.for('startPainting')]: Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([],function()    {
             return     this.dispatch('startPainting');
           })),     [Symbol.for('stopPainting')]: Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([],function()    {
             return     this.dispatch('stopPainting');
           })),     [Symbol.for('addPoint')]: Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([Bootstrap.Core.Patterns.variable()],function(data)    {
             return     this.dispatch('addPoint',data);
           })),     [Symbol.for('getStores')]: Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([],function()    {
             return     Object.freeze({
             [Symbol.for('graphic')]: GraphicStore
       });
           }))
       }));
           }));
    """

    assert_translation(ex_ast, js_code)
  end


  test "test array returns correctly" do
    ex_ast = quote do
      def my_func(x) do
        [x.a, x.b]
      end
    end

    js_code = """
    const my_func = Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([Bootstrap.Core.Patterns.variable()],function(x){
    return Object.freeze([Bootstrap.Core.Functions.call_property(x,'a'), Bootstrap.Core.Functions.call_property(x,'b')]);
    }));
    """

    assert_translation(ex_ast, js_code)
  end

  test "Elixir.Enum.member__qmark__ does not show up in translation" do
    ex_ast = quote do
       Enum.member?([1, 2, 3], 1)
    end

    js_code = """
    Elixir.Enum.member__qmark__(Object.freeze([1, 2, 3]), 1)
    """

    refute_translation(ex_ast, js_code)
  end

  test "pipe translates correctly" do<commit_msg>Update test to make sure Elixir.Enum.member__qmark__ does not show up in output<commit_after>defmodule ElixirScript.Translator.Bug.Test do
  use ExUnit.Case
  import ElixirScript.TestHelper

  test "Translate function with 0 arguments" do
    ex_ast = quote do
        def test do
          :atom
        end
    end

    js_code = """
      const test = Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([],function(){
        return Symbol.for('atom');
      }));
    """

    assert_translation(ex_ast, js_code)
  end

  test "Translate react element" do
    ex_ast = quote do
      React.createElement(
        React.Text,
        %{"style" => styles().welcome},
        "Welcome to React Native!"
      )
    end

    js_code = """
     React.createElement(React.Text,Object.freeze({
             style: Bootstrap.Core.Functions.call_property(styles,'welcome')
       }),'Welcome to React Native!')
    """

    assert_translation(ex_ast, js_code)

  end

  test "correctly not create 2 imports" do
    ex_ast = quote do
      defmodule App.Todo do
        JS.import JQuery, "jquery"
        JQuery.(e.target)
      end
    end

    js_code = """
    JQuery(Bootstrap.Core.Functions.call_property(e,'target'));
    """

    assert_translation(ex_ast, js_code)
  end

  test "correctly translate module names when used" do
    ex_ast = quote do
      @graphic_store App.Stores.GraphicStore.create_store()
    end

    js_code = """
      const graphic_store = Bootstrap.Core.Functions.call_property(App.Stores.GraphicStore, 'create_store');

    """

    assert_translation(ex_ast, js_code)
  end

  test "replace !" do
    ex_ast = quote do
      Bootstrap.Enum.fetch!(data, i)
    end

    js_code = """
      Bootstrap.Enum.fetch__emark__(data, i)
    """

    assert_translation(ex_ast, js_code)
  end

  test "chain calls correctly" do
    ex_ast = quote do
      :this.getRawCanvas().getContext("2d")
    end

    js_code = """
      Bootstrap.Core.Functions.call_property(this, 'getRawCanvas').getContext('2d')
    """

    assert_translation(ex_ast, js_code)


    ex_ast = quote do
      :this.getRawCanvas(one).get("fg").getContext("2d")
    end

    js_code = """
      this.getRawCanvas(one).get('fg').getContext('2d')
    """

    assert_translation(ex_ast, js_code)
  end

  test "correctly call multi-module functions" do
    ex_ast = quote do
      def getDispatcher() do
        DeLorean.Flux.createDispatcher(%{
          startPainting: fn() -> this.dispatch("startPainting") end,
          stopPainting: fn() -> this.dispatch("stopPainting") end,
          addPoint: fn(data) -> this.dispatch("addPoint", data) end,
          getStores: fn() -> %{ graphic: GraphicStore } end
        })
      end
    end


    js_code = """
     const getDispatcher = Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([],function()    {
             return     DeLorean.Flux.createDispatcher(Object.freeze({
             [Symbol.for('startPainting')]: Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([],function()    {
             return     this.dispatch('startPainting');
           })),     [Symbol.for('stopPainting')]: Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([],function()    {
             return     this.dispatch('stopPainting');
           })),     [Symbol.for('addPoint')]: Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([Bootstrap.Core.Patterns.variable()],function(data)    {
             return     this.dispatch('addPoint',data);
           })),     [Symbol.for('getStores')]: Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([],function()    {
             return     Object.freeze({
             [Symbol.for('graphic')]: GraphicStore
       });
           }))
       }));
           }));
    """

    assert_translation(ex_ast, js_code)
  end


  test "test array returns correctly" do
    ex_ast = quote do
      def my_func(x) do
        [x.a, x.b]
      end
    end

    js_code = """
    const my_func = Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([Bootstrap.Core.Patterns.variable()],function(x){
    return Object.freeze([Bootstrap.Core.Functions.call_property(x,'a'), Bootstrap.Core.Functions.call_property(x,'b')]);
    }));
    """

    assert_translation(ex_ast, js_code)
  end

  test "Elixir.Enum.member__qmark__ does not show up in translation" do
    ex_ast = quote do
       Enum.member?([1, 2, 3], 1)
    end

    js_code = """
    Elixir.Enum.member__qmark__
    """

    refute_translation(ex_ast, js_code)
  end

  test "pipe translates correctly" do
{'fraction_non_alphanumeric': 0.13080857547067146, 'fraction_numerical': 0.002611818478615736, 'mean_word_length': 2.667198723064645, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3369709', 'n_tokens_mistral': 3010, 'n_tokens_neox': 2880, 'n_words': 531}