from whitepy.lexerconstants import *
import whitepy.lexer as lexer
import unittest


class TestLexer(unittest.TestCase):

    def _get_lexer(self, line):
        return lexer.Lexer(line=line)

    def _valid_ws(self):
        return self._get_lexer("   \t\n")

    def test_get_int(self):
        lexer = self._valid_ws()
        r = lexer._get_int()
        assert r.get_type() == 'INT' and r.get_value() == 'POSITIVE 001'

    def test_get_token(self):
        lexer = self._valid_ws()
        lexer.pos = 1
        r = lexer._get_token(IMP_CONST)
        assert r.get_type() == 'STACK_MANIPULATION'

    def test_get_all_tokens(self):
        lexer = self._valid_ws()
        lexer.get_all_tokens()
        t = lexer.tokens
        assert t[0].get_type() is 'STACK_MANIPULATION' and \
            t[1].get_type() is 'PUSH' and t[2].get_type() is 'INT'

from nose.tools import *
from whitepy.lexerconstants import *
import unittest
import whitepy.lexer as lexer


class TestLexer(unittest.TestCase):

    def _get_lexer(self, line):
        return lexer.Lexer(line=line)

    def _sample_ws(self, ws_type):
        ws_samples = {
            'valid': "   \t\n",
            'invalid_int': "   \t"
        }
        return self._get_lexer(ws_samples[ws_type])

    def test_get_int(self):
        lexer = self._sample_ws('valid')
        r = lexer._get_int()
        assert r.get_type() == 'INT' and r.get_value() == 'POSITIVE 001'

    @raises(lexer.IntError)
    def test_invalid_int(self):
        lexer = self._sample_ws('invalid_int')
        lexer._get_int()

    def test_get_token(self):
        lexer = self._sample_ws('valid')
        lexer.pos = 1
        r = lexer._get_token(IMP_CONST)
        assert r.get_type() == 'STACK_MANIPULATION'

    def test_get_all_tokens(self):
        lexer = self._sample_ws('valid')
        lexer.get_all_tokens()
        t = lexer.tokens
        assert t[0].get_type() is 'STACK_MANIPULATION' and \
            t[1].get_type() is 'PUSH' and t[2].get_type() is 'INT'

Add test for invalid integer
Add test for invalid integer

A valid integer in Whitesapce ends with '\n', if an invalid integer is found the
code should raise a IntError exception.

As part of this, I have renamed `_valid_ws()` to `_sample_ws()`, which now takes
a argument for the type of whitespace needed.

+ from nose.tools import *
  from whitepy.lexerconstants import *
+ import unittest
  import whitepy.lexer as lexer
- import unittest
  
  
  class TestLexer(unittest.TestCase):
  
      def _get_lexer(self, line):
          return lexer.Lexer(line=line)
  
-     def _valid_ws(self):
-         return self._get_lexer("   \t\n")
+     def _sample_ws(self, ws_type):
+         ws_samples = {
+             'valid': "   \t\n",
+             'invalid_int': "   \t"
+         }
+         return self._get_lexer(ws_samples[ws_type])
  
      def test_get_int(self):
-         lexer = self._valid_ws()
+         lexer = self._sample_ws('valid')
          r = lexer._get_int()
          assert r.get_type() == 'INT' and r.get_value() == 'POSITIVE 001'
  
+     @raises(lexer.IntError)
+     def test_invalid_int(self):
+         lexer = self._sample_ws('invalid_int')
+         lexer._get_int()
+ 
      def test_get_token(self):
-         lexer = self._valid_ws()
+         lexer = self._sample_ws('valid')
          lexer.pos = 1
          r = lexer._get_token(IMP_CONST)
          assert r.get_type() == 'STACK_MANIPULATION'
  
      def test_get_all_tokens(self):
-         lexer = self._valid_ws()
+         lexer = self._sample_ws('valid')
          lexer.get_all_tokens()
          t = lexer.tokens
          assert t[0].get_type() is 'STACK_MANIPULATION' and \
              t[1].get_type() is 'PUSH' and t[2].get_type() is 'INT'
  
Add test for invalid integer
## Code Before:
from whitepy.lexerconstants import *
import whitepy.lexer as lexer
import unittest


class TestLexer(unittest.TestCase):

    def _get_lexer(self, line):
        return lexer.Lexer(line=line)

    def _valid_ws(self):
        return self._get_lexer("   \t\n")

    def test_get_int(self):
        lexer = self._valid_ws()
        r = lexer._get_int()
        assert r.get_type() == 'INT' and r.get_value() == 'POSITIVE 001'

    def test_get_token(self):
        lexer = self._valid_ws()
        lexer.pos = 1
        r = lexer._get_token(IMP_CONST)
        assert r.get_type() == 'STACK_MANIPULATION'

    def test_get_all_tokens(self):
        lexer = self._valid_ws()
        lexer.get_all_tokens()
        t = lexer.tokens
        assert t[0].get_type() is 'STACK_MANIPULATION' and \
            t[1].get_type() is 'PUSH' and t[2].get_type() is 'INT'

## Instruction:
Add test for invalid integer
## Code After:
from nose.tools import *
from whitepy.lexerconstants import *
import unittest
import whitepy.lexer as lexer


class TestLexer(unittest.TestCase):

    def _get_lexer(self, line):
        return lexer.Lexer(line=line)

    def _sample_ws(self, ws_type):
        ws_samples = {
            'valid': "   \t\n",
            'invalid_int': "   \t"
        }
        return self._get_lexer(ws_samples[ws_type])

    def test_get_int(self):
        lexer = self._sample_ws('valid')
        r = lexer._get_int()
        assert r.get_type() == 'INT' and r.get_value() == 'POSITIVE 001'

    @raises(lexer.IntError)
    def test_invalid_int(self):
        lexer = self._sample_ws('invalid_int')
        lexer._get_int()

    def test_get_token(self):
        lexer = self._sample_ws('valid')
        lexer.pos = 1
        r = lexer._get_token(IMP_CONST)
        assert r.get_type() == 'STACK_MANIPULATION'

    def test_get_all_tokens(self):
        lexer = self._sample_ws('valid')
        lexer.get_all_tokens()
        t = lexer.tokens
        assert t[0].get_type() is 'STACK_MANIPULATION' and \
            t[1].get_type() is 'PUSH' and t[2].get_type() is 'INT'
