lib: expectParseStateOn helper
This commit is contained in:
		
							parent
							
								
									763903ebda
								
							
						
					
					
						commit
						573a13fc27
					
				| @ -23,12 +23,13 @@ module Hledger.Utils.Test ( | |||||||
|   ,expectParseEqE |   ,expectParseEqE | ||||||
|   ,expectParseEqOn |   ,expectParseEqOn | ||||||
|   ,expectParseEqOnE |   ,expectParseEqOnE | ||||||
|  |   ,expectParseStateOn | ||||||
| )  | )  | ||||||
| where | where | ||||||
| 
 | 
 | ||||||
| import Control.Exception | import Control.Exception | ||||||
| import Control.Monad.Except (ExceptT, runExceptT) | import Control.Monad.Except (ExceptT, runExceptT) | ||||||
| import Control.Monad.State.Strict (StateT, evalStateT) | import Control.Monad.State.Strict (StateT, evalStateT, execStateT) | ||||||
| #if !(MIN_VERSION_base(4,11,0)) | #if !(MIN_VERSION_base(4,11,0)) | ||||||
| import Data.Monoid ((<>)) | import Data.Monoid ((<>)) | ||||||
| #endif | #endif | ||||||
| @ -106,7 +107,6 @@ is = flip expectEqPP | |||||||
| 
 | 
 | ||||||
| -- | Test that this stateful parser runnable in IO successfully parses  | -- | Test that this stateful parser runnable in IO successfully parses  | ||||||
| -- all of the given input text, showing the parse error if it fails.  | -- all of the given input text, showing the parse error if it fails.  | ||||||
| 
 |  | ||||||
| -- Suitable for hledger's JournalParser parsers. | -- Suitable for hledger's JournalParser parsers. | ||||||
| expectParse :: (Monoid st, Eq a, Show a, HasCallStack) =>  | expectParse :: (Monoid st, Eq a, Show a, HasCallStack) =>  | ||||||
|   StateT st (ParsecT CustomErr T.Text IO) a -> T.Text -> E.Test () |   StateT st (ParsecT CustomErr T.Text IO) a -> T.Text -> E.Test () | ||||||
| @ -216,3 +216,17 @@ expectParseEqOnE parser input f expected = do | |||||||
|              (expectEqPP expected . f) |              (expectEqPP expected . f) | ||||||
|              ep |              ep | ||||||
| 
 | 
 | ||||||
|  | -- | Run a stateful parser in IO like expectParse, then compare the | ||||||
|  | -- final state (the wrapped state, not megaparsec's internal state), | ||||||
|  | -- transformed by the given function, with the given expected value. | ||||||
|  | expectParseStateOn :: (HasCallStack, Monoid st, Eq b, Show b) => | ||||||
|  |      StateT st (ParsecT CustomErr T.Text IO) a | ||||||
|  |   -> T.Text | ||||||
|  |   -> (st -> b) | ||||||
|  |   -> b | ||||||
|  |   -> E.Test () | ||||||
|  | expectParseStateOn parser input f expected = do | ||||||
|  |   es <- E.io $ runParserT (execStateT (parser <* eof) mempty) "" input | ||||||
|  |   case es of | ||||||
|  |     Left err -> fail $ (++"\n") $ ("\nparse error at "++) $ customErrorBundlePretty err | ||||||
|  |     Right s  -> expectEqPP expected $ f s | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user