Skip to content

Commit

Permalink
Merge pull request #1057 from riganti/static-command-tests
Browse files Browse the repository at this point in the history
Static command tests
  • Loading branch information
acizmarik authored Jun 29, 2021
2 parents fd742af + 1d866de commit ae98099
Show file tree
Hide file tree
Showing 11 changed files with 937 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ public void JsTranslator_StringSplit_ArrayDelimiters_NoOptions(string binding, s
public void JsTranslator_StringArrayJoin(string binding, string delimiter)
{
var result = CompileBinding(binding, new[] { typeof(TestViewModel) });
Assert.AreEqual($"StringArray().join(\"{delimiter}\")", result);
Assert.AreEqual($"dotvvm.translations.string.join(StringArray(),\"{delimiter}\")", result);
}

[TestMethod]
Expand All @@ -837,7 +837,7 @@ public void JsTranslator_StringArrayJoin(string binding, string delimiter)
public void JsTranslator_StringEnumerableJoin(string binding, string delimiter)
{
var result = CompileBinding(binding, new[] { new NamespaceImport("System.Linq") }, new[] { typeof(TestViewModel) });
Assert.AreEqual($"StringArray().filter(function(item){{return ko.unwrap(item).length>2;}}).join(\"{delimiter}\")", result);
Assert.AreEqual($"dotvvm.translations.string.join(StringArray().filter(function(item){{return ko.unwrap(item).length>2;}}),\"{delimiter}\")", result);
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,10 @@ private void AddDefaultStringTranslations()

var joinStringArrayMethod = typeof(string).GetMethods(BindingFlags.Public | BindingFlags.Static)
.Where(m => m.Name == nameof(string.Join) && m.GetParameters().Length == 2 && m.GetParameters().Last().ParameterType == typeof(string[]) && m.GetParameters().First().ParameterType == typeof(string)).Single();
AddMethodTranslator(joinStringArrayMethod, translator: new GenericMethodCompiler(args => args[2].Member("join").Invoke(args[1])));
AddMethodTranslator(joinStringArrayMethod, translator: new GenericMethodCompiler(args => new JsIdentifierExpression("dotvvm").Member("translations").Member("string").Member("join").Invoke(args[2], args[1])));
var joinStringEnumerableMethod = typeof(string).GetMethods(BindingFlags.Public | BindingFlags.Static)
.Where(m => m.Name == nameof(string.Join) && m.GetParameters().Length == 2 && m.GetParameters().Last().ParameterType == typeof(IEnumerable<string>) && m.GetParameters().First().ParameterType == typeof(string)).Single();
AddMethodTranslator(joinStringEnumerableMethod, translator: new GenericMethodCompiler(args => args[2].Member("join").Invoke(args[1])));
AddMethodTranslator(joinStringEnumerableMethod, translator: new GenericMethodCompiler(args => new JsIdentifierExpression("dotvvm").Member("translations").Member("string").Member("join").Invoke(args[2], args[1])));

AddMethodTranslator(typeof(string), nameof(string.Replace), parameters: new[] { typeof(string), typeof(string) }, translator: new GenericMethodCompiler(
args => args[0].Member("split").Invoke(args[1]).Member("join").Invoke(args[2])));
Expand Down
5 changes: 5 additions & 0 deletions src/DotVVM.Framework/Resources/Scripts/utils/stringHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@

return tokens;
}

export function join<T>(elements: T[], delimiter: string): string {
let unwrappedElements = elements.map(ko.unwrap);
return unwrappedElements.join(delimiter);
}
1 change: 1 addition & 0 deletions src/DotVVM.Samples.Common/DotVVM.Samples.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<None Remove="Views\FeatureSamples\JavascriptTranslation\DictionaryIndexerTranslation.dothtml" />
<None Remove="Views\FeatureSamples\JavascriptTranslation\GenericMethodTranslation.dothtml" />
<None Remove="Views\FeatureSamples\JavascriptTranslation\ListMethodTranslations.dothtml" />
<None Remove="Views\FeatureSamples\JavascriptTranslation\StringMethodTranslations.dothtml" />
<None Remove="Views\FeatureSamples\JsDirectives\BasicSample.dothtml" />
<None Remove="Views\FeatureSamples\LambdaExpressions\ClientSideFiltering.dothtml" />
<None Remove="Views\FeatureSamples\LambdaExpressions\LambdaExpressions.dothtml" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DotVVM.Framework.ViewModel;

namespace DotVVM.Samples.Common.ViewModels.FeatureSamples.JavascriptTranslation
{
public class StringMethodTranslationsViewModel : DotvvmViewModelBase
{
public string Joke { get; set; } = "Why do Java programmers have to wear glasses? Because they do not C#.";

public string OperationResult { get; set; }
public string Value { get; set; } = "";

public string[] SplitArray { get; set; }
public int Index { get; set; }

public List<string> JoinList { get; set; } = new List<string> { "Real", "programmers", "count", "from", "0" };

public string[] JoinArray { get; set; } = { "Real", "programmers", "count", "from", "0" };
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<dot:Button Text="RemoveRange (2,5)" Click="{staticCommand: List.RemoveRange(2, 5)}" />
<dot:Button Text="Reverse" Click="{staticCommand: List.Reverse()}" />

<dot:GridView DataSource="{value: List}">
<dot:GridView DataSource="{value: List}" data-ui="grid">
<dot:GridViewTextColumn HeaderText="Value" ValueBinding="{value: _this}" />
</dot:GridView>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
@viewModel DotVVM.Samples.Common.ViewModels.FeatureSamples.JavascriptTranslation.StringMethodTranslationsViewModel, DotVVM.Samples.Common

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>

<h2 data-ui="main-text">{{value:Joke}}</h2>
<p>Value</p><dot:TextBox Text="{value: Value}" data-ui="textbox" />
<dot:Button Text="Contains(value)" Click="{staticCommand: OperationResult = Joke.Contains(Value).ToString()}" />
<dot:Button Text="EndsWith(value)" Click="{staticCommand: OperationResult = Joke.EndsWith(Value).ToString()}" />

<dot:Button Text="IndexOf(value)" Click="{staticCommand: Index = Joke.IndexOf(Value)}" />
<dot:Button Text="IndexOf(value,30)" Click="{staticCommand: Index = Joke.IndexOf(Value, 30)}" />

<dot:Button Text="IsNullOrEmpty(value)" Click="{staticCommand: OperationResult = String.IsNullOrEmpty(Value).ToString()}" />

<dot:Button Text="Join(., list)" Click="{staticCommand: OperationResult = String.Join('.', JoinList)}" />
<dot:Button Text="Join( JOIN , array)" Click="{staticCommand: OperationResult = String.Join(" JOIN ", JoinArray)}" />

<dot:Button Text="LastIndexOf(value)" Click="{staticCommand: Index = Joke.LastIndexOf(Value)}" />
<dot:Button Text="LastIndexOf(value, 30)" Click="{staticCommand: Index = Joke.LastIndexOf(Value, 30)}" />

<dot:Button Text="Replace(a, A)" Click="{staticCommand: OperationResult = Joke.Replace("a", "A")}" />

<dot:Button Text="Split((char)?)" Click="{staticCommand: SplitArray = Joke.Split('?')}" />
<dot:Button Text="Split((string)do)" Click="{staticCommand: SplitArray = Joke.Split("do")}" />

<dot:Button Text="ToLower()" Click="{staticCommand: OperationResult = Joke.ToLower()}" />
<dot:Button Text="ToUpper()" Click="{staticCommand: OperationResult = Joke.ToUpper()}" />

<p>Operation result</p><h2 data-ui="operation-result">{{value: OperationResult}}</h2>
<p>IndexOf result:</p> <h2 data-ui="index-result">{{value: Index}}</h2>

<dot:Repeater DataSource="{value: SplitArray}" data-ui="repeater">
<p>{{value: _this}}</p>
</dot:Repeater>

</body>
</html>


Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<div>
<h2>Source collection</h2>
<dot:GridView DataSource="{value: SourceCustomers}">
<dot:GridView DataSource="{value: SourceCustomers}" data-ui="source-grid">
<dot:GridViewTextColumn HeaderText="Id" ValueBinding="{value: Id}" />
<dot:GridViewTextColumn HeaderText="Name" ValueBinding="{value: Name}" />
<dot:GridViewTextColumn HeaderText="Category" ValueBinding="{value: Category}" />
Expand Down Expand Up @@ -75,7 +75,7 @@

<div>
<h2>Filtered collection</h2>
<dot:GridView DataSource="{value: FilteredCustomers}">
<dot:GridView DataSource="{value: FilteredCustomers}" data-ui="grid">
<dot:GridViewTextColumn HeaderText="Id" ValueBinding="{value: Id}" />
<dot:GridViewTextColumn HeaderText="Name" ValueBinding="{value: Name}" />
<dot:GridViewTextColumn HeaderText="Category" ValueBinding="{value: Category}" />
Expand All @@ -87,7 +87,7 @@

<div>
<h2>Other operations result</h2>
<dot:TextBox Text="{value: OperationResult}"/>
<dot:TextBox Text="{value: OperationResult}" data-ui="textbox"/>
</div>

</body>
Expand Down
Loading

0 comments on commit ae98099

Please sign in to comment.