public static void Throws <TException>(Action action, params string[] possibleMessages)
            where TException : Exception
        {
            try
            {
                action();

                Assert.Fail("Exception of type {0} expected. No exception thrown.", typeof(TException).Name);
            }
            catch (TException ex)
            {
                if (possibleMessages != null && possibleMessages.Length > 0)
                {
                    bool match = false;
                    foreach (string possibleMessage in possibleMessages)
                    {
                        if (StringAssert.Equals(possibleMessage, ex.Message))
                        {
                            match = true;
                            break;
                        }
                    }

                    if (!match)
                    {
                        throw new Exception("Unexpected exception message." + Environment.NewLine + "Expected one of: " + string.Join(Environment.NewLine, possibleMessages) + Environment.NewLine + "Got: " + ex.Message + Environment.NewLine + Environment.NewLine + ex, ex);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Exception of type {0} expected; got exception of type {1}.", typeof(TException).Name, ex.GetType().Name), ex);
            }
        }
Ejemplo n.º 2
0
        public static void AreEqual(ICollection <string> oneOfExpected, string actual)
        {
            bool match = oneOfExpected.Any(s => s == actual);

            if (!match)
            {
                Assert.Fail("Could not find match for actual value: {0}", actual);
            }
        }
        public void RegisterLicense_Sample()
        {
            try
            {
                string licenseKey = "your-license-key";

                License.RegisterLicense(licenseKey);
            }
            catch (JSchemaException)
            {
                return;
            }

            Assert.Fail();
        }
        private void GenerateSchemaAndSerializeFromType <T>(T value)
        {
            LicenseHelpers.ResetCounts(null);

            JSchemaGenerator generator = new JSchemaGenerator();

            generator.SchemaIdGenerationHandling = SchemaIdGenerationHandling.AssemblyQualifiedName;
            JSchema typeSchema = generator.Generate(typeof(T));
            string  schema     = typeSchema.ToString();

            string json  = JsonConvert.SerializeObject(value, Formatting.Indented);
            JToken token = JToken.ReadFrom(new JsonTextReader(new StringReader(json)));

            List <string> errors = new List <string>();

            token.Validate(typeSchema, (sender, args) => { errors.Add(args.Message); });

            if (errors.Count > 0)
            {
                Assert.Fail("Schema generated for type '{0}' is not valid." + Environment.NewLine + string.Join(Environment.NewLine, errors.ToArray()), typeof(T));
            }
        }