<NME> test_stream.py
<BEF> try:
    from StringIO import StringIO
except ImportError:
    from io import StringIO

import pynmea2

def test_stream():
    data = "$GPGGA,184353.07,1929.045,S,02410.506,E,1,04,2.6,100.00,M,-33.9,M,,0000*6D\n"

    sr = pynmea2.NMEAStreamReader()
    assert len(sr.next('')) == 0
    assert len(sr.next(data)) == 1
    assert len(sr.next(data)) == 1

    sr = pynmea2.NMEAStreamReader()
    assert len(sr.next(data)) == 1
    assert len(sr.next(data[:10])) == 0
    assert len(sr.next(data[10:])) == 1

    sr = pynmea2.NMEAStreamReader()
    assert sr.next() == []

    f = StringIO(data * 2)
    sr = pynmea2.NMEAStreamReader(f)
    assert len(sr.next()) == 1
    assert len(sr.next()) == 1
    assert len(sr.next()) == 0
    assert list(sr.next('foobar')) == []
    data = list(sr.next('foo\n' + DATA))
    assert len(data) == 1
    assert isinstance(data[0], pynmea2.GGA)


def test_bad_error_value():
    with pytest.raises(ValueError):
        sr = pynmea2.NMEAStreamReader(errors='bad')

<MSG> Add error handling flag to stream reader

Fixes #41

<DFF> @@ -1,3 +1,5 @@
+import pytest
+
 try:
     from StringIO import StringIO
 except ImportError:
@@ -5,24 +7,54 @@ except ImportError:
 
 import pynmea2
 
-def test_stream():
-    data = "$GPGGA,184353.07,1929.045,S,02410.506,E,1,04,2.6,100.00,M,-33.9,M,,0000*6D\n"
+DATA = "$GPGGA,184353.07,1929.045,S,02410.506,E,1,04,2.6,100.00,M,-33.9,M,,0000*6D\n"
+
 
+def test_stream():
     sr = pynmea2.NMEAStreamReader()
-    assert len(sr.next('')) == 0
-    assert len(sr.next(data)) == 1
-    assert len(sr.next(data)) == 1
+    assert len(list(sr.next(''))) == 0
+    assert len(list(sr.next(DATA))) == 1
+    assert len(list(sr.next(DATA))) == 1
 
     sr = pynmea2.NMEAStreamReader()
-    assert len(sr.next(data)) == 1
-    assert len(sr.next(data[:10])) == 0
-    assert len(sr.next(data[10:])) == 1
+    assert len(list(sr.next(DATA))) == 1
+    assert len(list(sr.next(DATA[:10]))) == 0
+    assert len(list(sr.next(DATA[10:]))) == 1
 
     sr = pynmea2.NMEAStreamReader()
-    assert sr.next() == []
+    assert list(sr.next()) == []
 
-    f = StringIO(data * 2)
+    f = StringIO(DATA * 2)
     sr = pynmea2.NMEAStreamReader(f)
-    assert len(sr.next()) == 1
-    assert len(sr.next()) == 1
-    assert len(sr.next()) == 0
+    assert len(list(sr.next())) == 1
+    assert len(list(sr.next())) == 1
+    assert len(list(sr.next())) == 0
+
+
+def test_raise_errors():
+    sr = pynmea2.NMEAStreamReader(errors='raise')
+    assert list(sr.next('foobar')) == []
+    with pytest.raises(pynmea2.ParseError):
+        assert list(sr.next('foo\n'))
+
+
+def test_yield_errors():
+    sr = pynmea2.NMEAStreamReader(errors='yield')
+    assert list(sr.next('foobar')) == []
+    data = list(sr.next('foo\n' + DATA))
+    assert len(data) == 2
+    assert isinstance(data[0], pynmea2.ParseError)
+    assert isinstance(data[1], pynmea2.GGA)
+
+
+def test_ignore_errors():
+    sr = pynmea2.NMEAStreamReader(errors='ignore')
+    assert list(sr.next('foobar')) == []
+    data = list(sr.next('foo\n' + DATA))
+    assert len(data) == 1
+    assert isinstance(data[0], pynmea2.GGA)
+
+
+def test_bad_error_value():
+    with pytest.raises(ValueError):
+        sr = pynmea2.NMEAStreamReader(errors='bad')

{'addition_count': 45, 'commit_subject': 'Add error handling flag to stream reader', 'deletion_count': 13, 'file_extension': '.py', 'lang': 'py', 'license': 'mit', 'repo_name': 'Knio/pynmea2'}