public void TestSimpleCondition()
        {
            string expression = "Name eq 'Australia'";
            var    countries  = languageProcessor.Find(allCountries, expression);

            countries.Should().Contain(allCountries.Last());
            countries.Count.Should().IsSameOrEqualTo(1);
        }
Пример #2
0
        static async Task Main()
        {
            LanguageProcessor languageProcessor = new LanguageProcessor();
            var allCountries = await InitCountriesAsync();

            Console.WriteLine("Data set:");
            Console.WriteLine(Encoding.UTF8.GetString(JsonSerializer.SerializeToUtf8Bytes(allCountries, options: new JsonSerializerOptions()
            {
                WriteIndented = true
            })));
            do
            {
                Console.WriteLine();
                Console.WriteLine("Please enter expression, e.g: Name eq 'Australia' or Population ge 100 and Population lt 201");
                Console.WriteLine($"Available operators: { string.Join(',', Enum.GetValues<Operator>())}");
                string expression = Console.ReadLine();
                if (string.IsNullOrEmpty(expression))
                {
                    break;
                }

                try
                {
                    var countries = languageProcessor.Find(allCountries, expression);
                    if (countries.Any())
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine("Matching countries:");
                        foreach (var country in countries)
                        {
                            Console.Write($"{country.Name}; ");
                        }
                        Console.ForegroundColor = ConsoleColor.White;
                        Console.WriteLine();
                    }
                    else
                    {
                        Console.WriteLine("Couldn't find any matching countries");
                    }
                }
                catch (ExpressionInvalidException ex)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine($"{ex.Message}: {ex.Details}");
                    Console.ForegroundColor = ConsoleColor.White;
                }
                catch (Exception)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine($"Something went wrong, please try again");
                    Console.ForegroundColor = ConsoleColor.White;
                }
            }while (true);
        }