private void CheckTestFixtureIsValid(TestFixture fixture) { Type fixtureType = fixture.FixtureType; string reason = null; if (!IsValidFixtureType(fixtureType, ref reason)) { fixture.RunState = RunState.NotRunnable; fixture.IgnoreReason = reason; } else if (!IsStaticClass(fixtureType)) { // Postpone checking for constructor with arguments till we invoke it // since Type.GetConstructor doesn't handle null arguments well. if (fixture.arguments == null || fixture.arguments.Length == 0) { if (Reflect.GetConstructor(fixtureType) == null) { fixture.RunState = RunState.NotRunnable; fixture.IgnoreReason = "No suitable constructor was found"; } } } }
private bool IsValidFixtureType(Type type, ref string reason) { if (type.IsAbstract) { reason = string.Format("{0} is an abstract class", type.FullName); return(false); } if (Reflect.GetConstructor(type) == null) { reason = string.Format("{0} does not have a valid constructor", type.FullName); return(false); } if (!NUnitFramework.CheckSetUpTearDownMethods(type, NUnitFramework.SetUpAttribute, ref reason) || !NUnitFramework.CheckSetUpTearDownMethods(type, NUnitFramework.TearDownAttribute, ref reason)) { return(false); } if (Reflect.HasMethodWithAttribute(type, NUnitFramework.FixtureSetUpAttribute, true)) { reason = "TestFixtureSetUp method not allowed on a SetUpFixture"; return(false); } if (Reflect.HasMethodWithAttribute(type, NUnitFramework.FixtureTearDownAttribute, true)) { reason = "TestFixtureTearDown method not allowed on a SetUpFixture"; return(false); } return(true); }
private void CheckTestFixtureIsValid(TestFixture fixture) { Type fixtureType = fixture.FixtureType; string reason = null; if (!IsValidFixtureType(fixtureType, ref reason)) { fixture.RunState = RunState.NotRunnable; fixture.IgnoreReason = reason; } else if (!IsStaticClass(fixtureType)) { object[] args = fixture.arguments; ConstructorInfo ctor = args == null || args.Length == 0 ? Reflect.GetConstructor(fixtureType) : Reflect.GetConstructor(fixtureType, Type.GetTypeArray(args)); if (ctor == null) { fixture.RunState = RunState.NotRunnable; fixture.IgnoreReason = "No suitable constructor was found"; } } }
Expression IParserFragmentEmitter <object, Token, char, long> .CreateTerminal(SymbolId symbolId, ParameterExpression varParser, ParameterExpression varTokenValue, ParameterExpression varPosition) { return(Expression.New( Reflect.GetConstructor(() => new Token(default(SymbolId), default(string))), Expression.New( Reflect.GetConstructor(() => new SymbolId(default(int))), Expression.Constant(symbolId.ToInt32())), Expression.Call( Reflect.GetStaticMethod(() => default(IEnumerable <char>).AsString()), Expression.Convert( varTokenValue, typeof(IEnumerable <char>))))); }
Expression IParserFragmentEmitter <object, Token, char, long> .CreateNonterminal(ProductionRule rule, ParameterExpression varParser, ParameterExpression[] varsProductionNodes) { if (varsProductionNodes.Length == 1) { return(varsProductionNodes[0]); } return(Expression.New( Reflect.GetConstructor(() => new Token(default(SymbolId), default(IEnumerable <Token>))), Expression.New( Reflect.GetConstructor(() => new SymbolId(default(int))), Expression.Constant(rule.ProductionSymbolId.ToInt32())), Expression.NewArrayInit(typeof(Token), varsProductionNodes))); }
public static Expression <Func <TLetter, LetterId> > CreateExpression(AlphabetBuilder <TLetter> builder, LetterId?defaultLetter = null) { var ranges = builder.AlphabetById.SelectMany(p => p.Value.Select(r => new KeyValuePair <Range <TLetter>, LetterId>(r, p.Key))).Where(p => p.Value != defaultLetter).OrderBy(p => p.Key.From).ToList(); var paramValue = Expression.Parameter(typeof(TLetter), "value"); var defaultExpression = defaultLetter.HasValue ? (Expression)Expression.Constant(defaultLetter.Value.ToInt32()) : Expression.Throw( Expression.New( Reflect.GetConstructor(() => new InvalidOperationException())), typeof(int)); var body = Expression.New(LetterId_Ctor, BinaryCompare(ranges, 0, ranges.Count, paramValue, defaultExpression)); return(Expression.Lambda <Func <TLetter, LetterId> >(body, paramValue)); }
/// <summary> /// Virtual method that returns true if the fixture type is valid /// for use by the builder. If not, it returns false and sets /// reason to an appropriate message. As implemented in this class, /// the method checks that a default constructor is available. You /// may override this method in a derived class in order to make /// different or additional checks. /// </summary> /// <param name="fixtureType">The fixture type</param> /// <param name="reason">The reason this fixture is not valid</param> /// <returns>True if the fixture type is valid, false if not</returns> protected virtual bool IsValidFixtureType(Type fixtureType, ref string reason) { if (fixtureType.IsAbstract) { reason = string.Format("{0} is an abstract class", fixtureType.FullName); return(false); } if (Reflect.GetConstructor(fixtureType) == null) { reason = string.Format("{0} does not have a valid constructor", fixtureType.FullName); return(false); } return(true); }
private static Expression CreateLetterStateMachine(Dfa <LetterId> dfa, ParameterExpression paramState, Expression exprInput) { var ctor_IdDfaStateLetter = Reflect.GetConstructor(() => new Id <DfaState <LetterId> >(default(int))); var meth_LetterId_ToInt32 = Reflect <LetterId> .GetMethod(i => i.ToInt32()); var varInput = Expression.Variable(typeof(int), "input"); var varResult = Expression.Variable(typeof(SymbolId?), "result"); return(Expression.Block( typeof(SymbolId?), new[] { varInput, varResult }, Expression.Assign(varInput, exprInput is NewExpression exprInputNew && (exprInputNew.Constructor == ctor_LetterId) ? exprInputNew.Arguments.Single() : Expression.Call(exprInput, meth_LetterId_ToInt32)), Expression.Assign( paramState, Expression.New(ctor_IdDfaStateLetter, Expression.Switch( Expression.Call(paramState, Reflect <Id <DfaState <LetterId> > > .GetMethod(i => i.ToInt32())), Expression.Throw(new_InvalidOperationException, typeof(int)), dfa.States.OrderBy(s => s.Id).Select(state => Expression.SwitchCase( Expression.Switch( varInput, Expression.Constant(Dfa <LetterId> .Reject.ToInt32()), state .Transitions .GroupBy(p => p.Value.ToInt32(), p => p.Key) .Select(g => Expression.SwitchCase(dfa.SymbolStates.TryGetValue(new Id <DfaState <LetterId> >(g.Key), out var symbolId) ? Expression.Block( Expression.Assign(varResult, Expression.New(ctor_NullableSymbolId, Expression.New(ctor_SymbolId, Expression.Constant((int)symbolId)))), Expression.Constant(g.Key)) : (Expression)Expression.Constant(g.Key), g.SelectMany(r => r.Expand()).Select(i => Expression.Constant(i.ToInt32())))) .ToArray()), Expression.Constant(state.Id.ToInt32()) )).ToArray()))), varResult)); }
public void GetConstructor() { Assert.IsNotNull(Reflect.GetConstructor(myType)); }
public static Expression <Action <TParser, ParserContextBase <TAstNode, TInput, TPosition>, SymbolId, Capture <TInput>, TPosition> > CreateExpression <TParser, TAstNode, TInput, TPosition>(LalrTable table, IParserFragmentEmitter <TParser, TAstNode, TInput, TPosition> fragmentEmitter, Func <SymbolId, string> resolver = null) { var paramParser = Expression.Parameter(typeof(TParser), "parser"); var paramContext = Expression.Parameter(typeof(ParserContextBase <TAstNode, TInput, TPosition>), "context"); var paramTokenSymbolId = Expression.Parameter(typeof(SymbolId), "tokenSymbolId"); var paramTokenValue = Expression.Parameter(typeof(Capture <TInput>), "tokenValue"); var paramPosition = Expression.Parameter(typeof(TPosition), "position"); var varInitialState = Expression.Variable(typeof(ParserState <TAstNode>), "initialState"); var exprCurrentState = Expression.Field(paramContext, Reflect <ParserContext <TAstNode, TInput, TPosition> > .GetField(c => c.currentState)); var lblBreak = Expression.Label("Break"); var lblContinue = Expression.Label("Continue"); var ctor_ParserState = Reflect.GetConstructor(() => new ParserState <TAstNode>(default(TAstNode), default(int), default(ParserState <TAstNode>))); var prop_ParserState_State = Reflect <ParserState <TAstNode> > .GetProperty(s => s.State); var prop_ParserState_Node = Reflect <ParserState <TAstNode> > .GetProperty(s => s.Node); var prop_ParserState_Parent = Reflect <ParserState <TAstNode> > .GetProperty(s => s.Parent); var meth_Accept = Reflect <ParserContext <TAstNode, TInput, TPosition> > .GetMethod(c => c.Accept(default(TAstNode))); var meth_SyntaxError = Reflect <ParserContext <TAstNode, TInput, TPosition> > .GetMethod(c => c.SyntaxError(default(SymbolId), default(Capture <TInput>), default(TPosition), default(IEnumerable <SymbolId>))); Expression DoShift(SymbolId symbolId, ShiftAction action) { return(Expression.Block(typeof(void), Expression.Assign( exprCurrentState, Expression.New( ctor_ParserState, fragmentEmitter.CreateTerminal(symbolId, paramParser, paramTokenValue, paramPosition), Expression.Constant(action.NewState), exprCurrentState)), Expression.Break(lblBreak))); } Expression DoReduce(ReduceSingleAction action) { var varCurrentState = Expression.Variable(typeof(ParserState <TAstNode>), "currentState"); var varsNodes = action.ProductionRule.RuleSymbolIds.Select(s => Expression.Variable(typeof(TAstNode), s.ToString(resolver))).ToArray(); var body = new List <Expression>(varsNodes.Length * 2 + 3); body.Add(Expression.Assign( varCurrentState, exprCurrentState)); for (var i = varsNodes.Length - 1; i >= 0; i--) { body.Add(Expression.Assign( varsNodes[i], Expression.Property( varCurrentState, prop_ParserState_Node))); body.Add(Expression.Assign( varCurrentState, Expression.Property( varCurrentState, prop_ParserState_Parent))); } body.Add(Expression.Assign( exprCurrentState, Expression.New( ctor_ParserState, fragmentEmitter.CreateNonterminal(action.ProductionRule, paramParser, varsNodes), Expression.Switch( Expression.Property( varCurrentState, prop_ParserState_State), Expression.Throw( Expression.New(ctor_InvalidOperationException), typeof(int)), table.Action .Where(a => (a.Value.Type == ActionType.Goto) && (a.Key.Value == action.ProductionRule.ProductionSymbolId)) .Select(a => Expression.SwitchCase( Expression.Constant(((GotoAction)a.Value).NewState), Expression.Constant(a.Key.State) )).ToArray()), varCurrentState))); body.Add(Expression.Continue(lblContinue)); return(Expression.Block(typeof(void), varsNodes.Append(varCurrentState), body)); } Expression DoAccept() { return(Expression.Block(typeof(void), Expression.Call( paramContext, meth_Accept, Expression.Property( exprCurrentState, prop_ParserState_Node)), Expression.Assign( exprCurrentState, Expression.Property( exprCurrentState, prop_ParserState_Parent)), Expression.Break(lblBreak))); } Expression DoThrow() { throw new InvalidOperationException("Internal error: unexpected action type"); } Expression DoSyntaxError() { var varSimulatedState = Expression.Variable(typeof(ParserState <TAstNode>), "simulatedState"); var lblSimulationBreak = Expression.Label(typeof(bool), "simulationBreak"); Expression DoSimulateReduce(ProductionRule rule) { Expression exprNewSimulatedState = Expression.Assign( varSimulatedState, Expression.New( ctor_ParserState, Expression.Default(typeof(TAstNode)), Expression.Switch( Expression.Property( varSimulatedState, prop_ParserState_State), Expression.Break( lblSimulationBreak, Expression.Constant(false), typeof(int)), table.Action .Where(a => (a.Key.Value == rule.ProductionSymbolId) && (a.Value.Type == ActionType.Goto)) .GroupBy(a => ((GotoAction)a.Value).NewState) .Select(g => Expression.SwitchCase( Expression.Constant(g.Key), g.Select(a => Expression.Constant(a.Key.State)))).ToArray()), varSimulatedState)); return(rule.RuleSymbolIds.Count == 0 ? exprNewSimulatedState : Expression.Block( Expression.Assign(varSimulatedState, rule.RuleSymbolIds.Aggregate <SymbolId, Expression>(varSimulatedState, (expression, id) => Expression.Property(expression, prop_ParserState_Parent))), exprNewSimulatedState)); } var varExpectedTokens = Expression.Variable(typeof(List <SymbolId>), "expectedTokens"); var terminalSymbolIds = table.Action .Where(a => (a.Value.Type == ActionType.Accept) || (a.Value.Type == ActionType.Shift)) .Select(a => a.Key.Value) .Distinct() .OrderBy(id => id.ToInt32()) .ToArray(); var body = new List <Expression>(terminalSymbolIds.Length + 2); body.Add(Expression.Assign( varExpectedTokens, Expression.New( ctor_ListOfSymbolId, Expression.Constant(terminalSymbolIds.Length)))); foreach (var symbolId in terminalSymbolIds) { body.Add(Expression.IfThen( Expression.Block(typeof(bool), new[] { varSimulatedState }, Expression.Assign( varSimulatedState, exprCurrentState), Expression.Loop( Expression.Switch(typeof(void), Expression.Property( varSimulatedState, prop_ParserState_State), Expression.Break(lblSimulationBreak, Expression.Constant(false)), null, table.Action .Where(a => (a.Key.Value == symbolId) && (a.Value.Type == ActionType.Reduce)) .GroupBy(a => ((ReduceSingleAction)a.Value).ProductionRule) .Select(g => Expression.SwitchCase( DoSimulateReduce(g.Key), g.Select(a => Expression.Constant(a.Key.State)))) .Append(Expression.SwitchCase( Expression.Break(lblSimulationBreak, Expression.Constant(true)), table.Action .Where(a => (a.Key.Value == symbolId) && ((a.Value.Type == ActionType.Accept) || (a.Value.Type == ActionType.Shift))) .Select(a => Expression.Constant(a.Key.State)) )).ToArray()), lblSimulationBreak)), Expression.Call( varExpectedTokens, meth_ListOfSymbolId_Add, Expression.New( ctor_SymbolId_Int32, Expression.Constant(symbolId.ToInt32()))))); } body.Add(Expression.Convert( varExpectedTokens, typeof(IEnumerable <SymbolId>))); var exprExpectedSymbolIds = Expression.Block(typeof(IEnumerable <SymbolId>), new[] { varExpectedTokens }, body); var exprCustomSyntaxError = fragmentEmitter.SyntaxError(paramParser, paramTokenSymbolId, paramTokenValue, paramPosition, exprExpectedSymbolIds); if (exprCustomSyntaxError?.Type == typeof(bool)) { exprCustomSyntaxError = Expression.IfThen( exprCustomSyntaxError, Expression.Continue(lblContinue)); } return(Expression.Block(typeof(void), Expression.Assign( exprCurrentState, varInitialState), exprCustomSyntaxError ?? Expression.Call( paramContext, meth_SyntaxError, paramTokenSymbolId, paramTokenValue, paramPosition, exprExpectedSymbolIds), Expression.Break(lblBreak))); } return(Expression.Lambda <Action <TParser, ParserContextBase <TAstNode, TInput, TPosition>, SymbolId, Capture <TInput>, TPosition> >( Expression.Block(typeof(void), new[] { varInitialState }, Expression.Assign( varInitialState, exprCurrentState), Expression.Loop( Expression.Block(typeof(void), Expression.Switch( Expression.Property( exprCurrentState, prop_ParserState_State), table.Action .Where(a => (a.Value.Type == ActionType.Shift) || (a.Value.Type == ActionType.Reduce) || (a.Value.Type == ActionType.Accept)) .GroupBy(a => a.Key.State) .Select(g => Expression.SwitchCase( Expression.Switch( Expression.Call( paramTokenSymbolId, meth_SymbolId_ToInt32), g.Select(a => Expression.SwitchCase( a.Value.Type == ActionType.Shift ? DoShift(a.Key.Value, (ShiftAction)a.Value) : a.Value.Type == ActionType.Reduce ? DoReduce((ReduceSingleAction)a.Value) : a.Value.Type == ActionType.Accept ? DoAccept() : DoThrow(), Expression.Constant(a.Key.Value.ToInt32()))).ToArray()), Expression.Constant(g.Key))).ToArray()), DoSyntaxError() ), lblBreak, lblContinue)), paramParser, paramContext, paramTokenSymbolId, paramTokenValue, paramPosition)); }