|
| 1 | +// |
| 2 | +// Copyright (c) 2019-2026 Angouri. |
| 3 | +// AngouriMath is licensed under MIT. |
| 4 | +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. |
| 5 | +// Website: https://am.angouri.org. |
| 6 | +// |
| 7 | + |
| 8 | +using AngouriMath; |
| 9 | +using AngouriMath.Core.Exceptions; |
| 10 | +using AngouriMath.Extensions; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +namespace AngouriMath.Tests.Convenience |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// <see cref="MathS.Parse"/> does not throw: a call the grammar refuses, a name refused by name or a function given |
| 17 | + /// the wrong number of arguments, is the failure it describes. It threw the refusal's exception, and so did |
| 18 | + /// <c>Entity.TryParse</c>. <see cref="MathS.FromString(string)"/> still throws it, with its own type. |
| 19 | + /// https://github.com/asc-community/AngouriMath/issues/2498 |
| 20 | + /// </summary> |
| 21 | + [Trait("Area", "Convenience")] |
| 22 | + public sealed class ParseReturnsARefusalTest |
| 23 | + { |
| 24 | + [Theory] |
| 25 | + [InlineData("factorize(360)", "factorize")] |
| 26 | + [InlineData("nextprime(10)", "nextprime")] |
| 27 | + [InlineData("norm(v, p, q)", "norm")] |
| 28 | + [InlineData("sin(x, y)", "sin")] |
| 29 | + public void ParseAnswersTheRefusal(string source, string named) |
| 30 | + { |
| 31 | + var reason = MathS.Parse(source).Switch( |
| 32 | + parsed => throw new Xunit.Sdk.XunitException($"{source} parsed as {parsed}"), |
| 33 | + failure => failure.Reason.Switch( |
| 34 | + unknown => unknown.Reason, |
| 35 | + missingOperator => throw new Xunit.Sdk.XunitException("a missing operator"), |
| 36 | + internalError => throw new Xunit.Sdk.XunitException($"an internal error: {internalError.Details}"))); |
| 37 | + Assert.Contains(named, reason); |
| 38 | + } |
| 39 | + |
| 40 | + [Theory] |
| 41 | + [InlineData("factorize(360)")] |
| 42 | + [InlineData("norm(v, p, q)")] |
| 43 | + public void TryParseSaysNo(string source) |
| 44 | + => Assert.False(Entity.TryParse(source, null, out _)); |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public void FromStringStillThrowsTheRefusal() |
| 48 | + { |
| 49 | + Assert.Throws<UnrecognizedFunctionParseException>(() => "factorize(360)".ToEntity()); |
| 50 | + Assert.Throws<FunctionArgumentCountException>(() => "norm(v, p, q)".ToEntity()); |
| 51 | + } |
| 52 | + |
| 53 | + [Fact] |
| 54 | + public void AValidInputStillParses() |
| 55 | + => Assert.Equal("sin(x) + 1".ToEntity(), MathS.Parse("sin(x) + 1").Switch(parsed => parsed, _ => null!)); |
| 56 | + } |
| 57 | +} |
0 commit comments