Skip to content

Commit

Permalink
Add the MTAccent atom class.
Browse files Browse the repository at this point in the history
  • Loading branch information
kostub committed Jul 21, 2016
1 parent 4d5549f commit a68c99d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
14 changes: 13 additions & 1 deletion iosMath/lib/MTMathList.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,23 @@ typedef NS_ENUM(NSUInteger, MTMathAtomType)

@end

/** An atom with an accent. */
@interface MTAccent : MTMathAtom

/** Creates a new `MTAccent` with the given value as the accent.
*/
- (instancetype)initWithValue:(NSString*) value NS_DESIGNATED_INITIALIZER;

/// The mathlist under the accent.
@property (nonatomic, nullable) MTMathList* innerList;

@end

/** A representation of a list of math objects.
This list can be constructed directly or built with
the help of the MTMathListBuilder. It is not required that the mathematics represented make sense
(i.e. this cn represent something like "x 2 = +". This list can be used for display using MTLine
(i.e. this can represent something like "x 2 = +". This list can be used for display using MTLine
or can be a list of tokens to be used by a parser after finalizedMathList is called.
@note This class is for ADVANCED usage only.
Expand Down
36 changes: 36 additions & 0 deletions iosMath/lib/MTMathList.m
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,35 @@ - (id)copyWithZone:(NSZone *)zone

@end

#pragma mark - MTAccent

@implementation MTAccent

- (instancetype)initWithValue:(NSString *)value
{
self = [super initWithType:kMTMathAtomAccent value:value];
return self;
}

- (instancetype)initWithType:(MTMathAtomType)type value:(NSString *)value
{
if (type == kMTMathAtomAccent) {
return [self initWithValue:value];
}
@throw [NSException exceptionWithName:@"InvalidMethod"
reason:@"[MTAccent initWithType:value:] cannot be called. Use [MTAccent initWithValue:] instead."
userInfo:nil];
}

- (id)copyWithZone:(NSZone *)zone
{
MTUnderLine* op = [super copyWithZone:zone];
op.innerList = [self.innerList copyWithZone:zone];
return op;
}

@end

#pragma mark - MTMathList

@implementation MTMathList {
Expand Down Expand Up @@ -648,6 +677,13 @@ - (MTMathList *)finalized
break;
}

case kMTMathAtomAccent: {
MTAccent* accent = (MTAccent*) atom;
MTAccent* newAccent = (MTAccent*) newNode;
newAccent.innerList = accent.innerList.finalized;
break;
}

default:
break;
}
Expand Down
18 changes: 18 additions & 0 deletions iosMathTests/MTMathListTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -512,4 +512,22 @@ - (void) testCopyUnderline
[MTMathListTest checkListCopy:copy.innerList original:under.innerList forTest:self];
}

- (void) testCopyAcccent
{
MTMathList* list = [[MTMathList alloc] init];
MTMathAtom* atom = [MTMathAtomFactory placeholder];
MTMathAtom* atom2 = [MTMathAtomFactory times];
MTMathAtom* atom3 = [MTMathAtomFactory divide];
[list addAtom:atom];
[list addAtom:atom2];
[list addAtom:atom3];

MTAccent* accent = [[MTAccent alloc] initWithValue:@"^"];
XCTAssertEqual(accent.type, kMTMathAtomAccent);
accent.innerList = list;

MTOverLine* copy = [accent copy];
[MTMathListTest checkAtomCopy:copy original:accent forTest:self];
[MTMathListTest checkListCopy:copy.innerList original:accent.innerList forTest:self];
}
@end

0 comments on commit a68c99d

Please sign in to comment.