Skip to content
This repository has been archived by the owner on Sep 15, 2024. It is now read-only.

Commit

Permalink
Fix the bug of ReactiveView's providing wrong argument to map and `…
Browse files Browse the repository at this point in the history
…pipe`
  • Loading branch information
mysticfall committed Oct 16, 2020
1 parent ea13a6c commit c3faca5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 31 deletions.
9 changes: 1 addition & 8 deletions alleycat/reactive/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from returns.context import RequiresContext
from rx import Observable
from rx import operators as ops

from . import ReactiveValue
from .value import Modifier
Expand All @@ -20,13 +19,7 @@ def __init__(self, init_value: RequiresContext[Observable, Any], read_only=True)
self._init_value = init_value

def pipe(self, modifiers: Callable[[Any], Tuple[Modifier, ...]]) -> ReactiveView:
return ReactiveView(self.context.map(lambda o: o.pipe(*(modifiers(o)))), self.read_only)

def with_instance(self) -> ReactiveView[Tuple[Any, T]]:
context: RequiresContext[Observable, Any] = \
RequiresContext(lambda i: self.context(i).pipe(ops.map(lambda v: (i, v))))

return ReactiveView(context, self.read_only)
return ReactiveView(RequiresContext(lambda i: self.context(i).pipe(*(modifiers(i)))), self.read_only)

def _create_data(self, obj: Any) -> ReactiveValue.Data:
assert obj is not None
Expand Down
6 changes: 6 additions & 0 deletions tests/test_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,12 @@ class Fixture:

song: RP[str] = name.map(lambda _, n: f"Who's afraid of a big bad {n}?")

instance: RP[Any] = name.map(lambda o, _: o)

fixture = Fixture()

self.assertEqual("Who's afraid of a big bad wolf?", fixture.song)
self.assertEqual(fixture, fixture.instance)

fixture.song = "cat"

Expand All @@ -210,9 +213,12 @@ class Fixture:
song: RP[str] = name.pipe(
lambda _: (ops.map(lambda n: f"Who's afraid of a big bad {n}?"), ops.map(str.upper)))

instance: RP[Any] = name.pipe(lambda o: (ops.map(lambda _: o),))

fixture = Fixture()

self.assertEqual("WHO'S AFRAID OF A BIG BAD WOLF?", fixture.song)
self.assertEqual(fixture, fixture.instance)

fixture.song = "cat"

Expand Down
30 changes: 7 additions & 23 deletions tests/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import rx
from returns.context import RequiresContext
from rx import operators as ops
from rx.subject import BehaviorSubject, Subject
from rx.subject import BehaviorSubject

from alleycat.reactive import ReactiveView, functions as rv, RV

Expand Down Expand Up @@ -148,10 +148,13 @@ class Fixture:

song: RV[str] = name.map(lambda _, n: f"Who's afraid of a big bad {n}?")

instance: RV[Any] = name.map(lambda o, _: o)

fixture = Fixture()

self.assertEqual(False, Fixture.song.read_only)
self.assertEqual("Who's afraid of a big bad wolf?", fixture.song)
self.assertEqual(fixture, fixture.instance)

source.on_next("cat")

Expand All @@ -165,37 +168,18 @@ class Fixture:

song: RV[str] = name.pipe(lambda _: (ops.map(lambda n: f"Who's afraid of a big bad {n}?"),))

instance: RV[Any] = name.pipe(lambda o: (ops.map(lambda _: o),))

fixture = Fixture()

self.assertEqual(False, Fixture.song.read_only)
self.assertEqual("Who's afraid of a big bad wolf?", fixture.song)
self.assertEqual(fixture, fixture.instance)

source.on_next("cat")

self.assertEqual("Who's afraid of a big bad cat?", fixture.song)

def test_with_instance(self):
source = Subject()

class Fixture:
name: str = "a little good kitty"

times: RV[int] = ReactiveView(RequiresContext.from_value(source))

lyric: RV[str] = times.with_instance().map(
lambda _, v: "Who's afraid of " + (", ".join(map(lambda _: v[0].name, range(v[1]))) + "?"))

fixture = Fixture()

source.on_next(1)

self.assertEqual("Who's afraid of a little good kitty?", fixture.lyric)

fixture.name = "a big bad wolf"
source.on_next(3)

self.assertEqual("Who's afraid of a big bad wolf, a big bad wolf, a big bad wolf?", fixture.lyric)

def test_extend(self):
counter = BehaviorSubject(1)

Expand Down

0 comments on commit c3faca5

Please sign in to comment.